There are files:

class1.cpp class1.h class2.cpp class2.h class3.cpp class3.h main.cpp 

In main.cpp

 #include "class1.h" #include "class2.h" #include "class3.h" 

In each of the classes used

 #include <iostream> #include <string> 

Is there any way to get rid of the heaps of the classes in each of the classes?

    2 answers 2

    1. To begin, learn how to arrange tags. C ++ and C ++ are different tags.
    2. And why reduce the number of inclusions? Purely hypothetically, there are two global approaches: either to make a super incli bly file in which to include all dependencies that are required for a module or for a project, or to include only what is necessary for this source file in each of the source files. Pros and cons are in each of the ways. For example, the advantage of the second method is that class files 1-3 are obtained independent of each other. This may be relevant in a situation if they need to be distributed to other developers or some kind of library is being created. The first method is good for small projects that consist of several "strongly" related components. Regarding the compilation time - you can not guess what is better. It may be better both one and the other depending on the situation, all the more so on the refresh rate of the headers. By the way, if one big leader is used, then when changing it, you will have to rebuild everything that depends on it. If you change one small header included in two or three files, only these two or three files will be re-assembled.

    Thus, it turns out that in each particular case the programmer must decide for himself how it is more convenient for him to build a program. In the end, there are absolutely no hard restrictions on how and what to do. Well, except that the compiler may require special header files if you use precompiled headers (remember msvc with its stupid stdafx.h )

    • Thank you very much! msvc ... I have gcc ... - avengerweb
    • 2
      not at all in the topic, and in general it looks like tediousness, but still - where did the word "Header" come from? Heder, from the word "head" - DreamChild
    • @DreamChild probably because it’s nicer to Russian. Perhaps somehow echoes healer = "healer". I do not know, in short, for sure. - gecube
    • @gecube, but on the contrary, I don’t like the hder - avp
    • @avp me hider also somehow cuts off hearing. Although its appearance is generally understandable - the rules of the transcription of the English language are rather clumsy and full of various nuances. In particular, the combination of ea in most cases is read precisely as a protracted one and , however, the word head reads like e , since it is followed by the letter d , which for many people leaves the head - DreamChild

    If class headers are embedded into each other, then the headers used in the upper files are implicitly connected in the "lower" in the hierarchy file, so that they can be omitted separately. In addition, the "good style" (tm) dictates the need to get rid of the inclusions in headers (transferring them to * .cpp files), using forward declaration where this is possible. And this is possible in cases where a pointer to a class is used in the header file, without referring to its members (and other situations in which the size of the object is needed). Thus, we can minimize implicit inclusion (in some cases, redundant inclusion). Specific example:

     //------------- // SomeType.h //------------- class OtherType; class SomeType { int i_; OtherType* ptr_; public: SomeType(int a, OtherType* pObj); void Action(OtherType* pObj); }; //------------- // SomeType.cpp //------------- #include "OtherType.h" SomeType::SomeType(int a, OtherType* pObj); : i_(a), ptr_(pObj) { } void SomeType::Action(OtherType* pObj) { ... }