C/C++ C++: What to do with all these classes

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Classes
AI Thread Summary
Organizing header and implementation files for related classes is crucial for efficient programming. Compiling these files into a library allows for easier linking in various programs, reducing the need for excessive #include directives. A suggested approach is to create a "group_header.hpp" file that includes multiple headers, but caution is advised against using a single "kitchensink.h" file, as this can indicate poor software design. Instead, each header should include only the necessary dependencies, promoting better encapsulation and reducing compile-time dependencies. Excessive fan-out, where a single file requires numerous headers, can signal design issues and lead to longer build times. Properly structuring header files can streamline the development process and enhance code maintainability.
Saladsamurai
Messages
3,009
Reaction score
7
I am wondering, after one has created a bunch of header and implementation files for a bunch of related classes, what do you do with them? How can they be organized so that I do not have to #include " " an unreasonable amount of headers in different programs? I am googling this, but I am not quite sure what it is that I am looking for.

Thanks!
 
Technology news on Phys.org
I would compile them into a library file which you can link in whenever needed.
 
Code:
//file group_header.hpp
#ifndef GROUP_HEADER_NAME_GOES_HERE
#define GROUP_HEADER_NAME_GOES_HERE

//multiple headers included here
#include "header1.hpp"
#include <header2>
#include "header3.h"
//etc

#endif

Code:
//file main.cpp
#include "group_header.hpp"

int main() {}
 
You can have one #include "everything.h" that has #includes for all your other .h files.
 
Having a kitchensink.h or whole_enchilada.h is almost always a very bad idea. When I'm browsing through a package and see such a monster I take that as a sign of a poorly designed chunk of software.

Much better is to make your header files include the header files that are needed by that header. Example: Suppose file bar.h defines the class Bar, one of whose data members is of type Foo. Class Foo is defined in the file foo.h. In this case, the file bar.h should contain a #include "foo.h" directive. On the other hand, if the dependencies to class Foo in bar.h are only in the form of pointers or references to instances of Foo, then bar.h should not #include foo.h. All that is needed is a forward declaration of the class. In your source files, you should include only the headers that define the external functionality needed by that source file. You should not have to include the headers needed by those headers if you developed your header files correctly.

What if you need to include dozens and dozens of header files in a single file? That too is a sign of a bad design. It's called excessive fan-out. A fan-out of more than 7±21[/color] is a sign of problems. A fan-out in the dozens is a sign of big problems.1[/color]George Miller, "The Magical Number Seven, Plus or Minus Two: Some Limits on Our Capacity for Processing Information", Psychological Review 63 (2): 81–97 (1956).
 
zhermes said:
I would compile them into a library file which you can link in whenever needed.

What does that have to do with the 'headers' question?

Coin said:
You can have one #include "everything.h" that has #includes for all your other .h files.

You are unnecessarily increasing your build times. If you change even one header file, the whole project would be rebuilt even in incremental builds.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top