C++: What to do with all these classes

  • C/C++
  • Thread starter Saladsamurai
  • Start date
  • Tags
    Classes
In summary: Header files should only include the headers that define the external functionality needed by that source file. If you develop your header files correctly, you don't need to include the headers needed by those headers.
  • #1
Saladsamurai
3,020
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
  • #2
I would compile them into a library file which you can link in whenever needed.
 
  • #3
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() {}
 
  • #4
You can have one #include "everything.h" that has #includes for all your other .h files.
 
  • #5
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 is a sign of problems. A fan-out in the dozens is a sign of big problems.1George Miller, "The Magical Number Seven, Plus or Minus Two: Some Limits on Our Capacity for Processing Information", Psychological Review 63 (2): 81–97 (1956).
 
  • #6
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.
 

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

What is C++?

C++ is a programming language that allows developers to create high-performance applications and software. It is an extension of the C programming language and is commonly used in developing operating systems, video games, and other complex systems.

What are classes in C++?

In C++, classes are a fundamental building block used to create objects. They provide a template for objects and define their properties and behaviors. Classes contain data members and member functions, which can be accessed and used by objects created from the class.

How do I declare a class in C++?

To declare a class in C++, you use the keyword "class" followed by the name of the class. Inside the curly braces, you define the data members and member functions of the class. The declaration of a class usually goes in a header file, while the definition goes in a source file.

What is the difference between a class and an object in C++?

A class is a blueprint or template for objects, while an object is an instance of a class. In other words, a class defines the properties and behaviors of objects, and objects are created from the class and can be used to perform specific tasks.

How do I use classes in my C++ code?

To use classes in C++, you first need to declare and define the class. Then, you can create objects from the class and use them in your code by calling their member functions and accessing their data members. Classes can also be used to organize and structure your code, making it more efficient and easier to maintain.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
2
Views
708
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
5
Views
938
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
3K
Back
Top