0

I always arranged my C++ headers and source files this way:

prog.h

#include <iostream>
class Prog
{
    public:
        Prog(std::string);
        ~Prog();
        
        void printName();
        
    private:
        std::string name;
};

#include "prog.cpp"

prog.cpp

Prog::Prog(std::string n):
    name(n)
{
    std::cout << "Program \"" << name << "\" started.\n";
}

Prog::~Prog()
{
    std::cout << "Program \"" << name << "\" ended.\n";
}

void Prog::printName()
{
    std::cout << "Program name is: \"" << name << "\".\n";
}

main.cpp

#include "prog.h"

int main()
{
    Prog prog {"MyCalculator"};
    
    prog.printName();
    
    return 0;
}

But I recently discovered that it's common to arrange them this other way:

prog.h

class Prog
{
    public:
        Prog(std::string);
        ~Prog();
        
        void printName();
        
    private:
        std::string name;
};

prog.cpp

#include <iostream>
#include "prog.h"

Prog::Prog(std::string n):
    name(n)
{
    std::cout << "Program \"" << name << "\" started.\n";
}

Prog::~Prog()
{
    std::cout << "Program \"" << name << "\" ended.\n";
}

void Prog::printName()
{
    std::cout << "Program name is: \"" << name << "\".\n";
}

main.cpp

#include <iostream>
#include "prog.h"

int main()
{
    Prog prog {"MyCalculator"};
    
    prog.printName();
    
    return 0;
}

I find the first method more convenient, so is there any reason why I should prefer the second method over the first one?

DarkoNaito_09
  • 71
  • 2
  • 12
  • 1
    If you are asking why not include the source file at the end of a header, this would defeat the purpose of separating code between source and header files. – Mansoor Aug 22 '20 at 13:39
  • Aren't they separeted because it makes the project more organized? – DarkoNaito_09 Aug 22 '20 at 13:40
  • 3
    There are 2 very distinct questions here. 1) should you include source code in the header file, to which the answer is an enthusiastic "no", and 2) should you put the source code for a class in a different file than main(), to which the answer is an equally enthusiastic "yes". Either way, the "other way" is better. – William Pursell Aug 22 '20 at 13:46
  • @Mansoor but with my method, I'd never include the same header twice – DarkoNaito_09 Aug 22 '20 at 13:53
  • 2
    I think this may work with a small school project but does not work when you have 100 thousand lines of code and several hundred source files and the build times are 20+ minutes. I agree with @WilliamPursell – drescherjm Aug 22 '20 at 14:13
  • In the first use case, I'd change `prog.cpp` to `prog.inline.h`, and make sure to mark the method definitions as `inline`. In the second use case, `prog.h` should `#include `, because you should *include what you use*. – Eljay Aug 22 '20 at 15:09

2 Answers2

2

Header files shouldn't include source code.

  • I think the first method could provoke dependency errors in a complex enough project (for example, using circular dependencies), because don't separate declaration and definition.
  • The compilation time is faster in the second one, because you can compile each source file separately.

Also, you might find this question useful.

Miguel
  • 2,130
  • 1
  • 11
  • 26
  • Header files *are* source code, but they shouldn't emit anything. (Well, some *do* emit things, and that's arguably bad.) There are lots of constructs that don't emit anything: uninstantiated templates, inline functions, macros (...which are The Devil™), forward class declarations, class declarations, yada yada. – Eljay Aug 22 '20 at 15:20
  • 1
    Well, you are right. By source code I meant something that can be translated to object code... – Miguel Aug 22 '20 at 15:32
  • But with the second method the compiler would have to translate iostream for both files, while with the first one it would translate iostream only once. And the more .cpp files I have which use iostream stuff the more times the compiler would have to translate iostream – DarkoNaito_09 Aug 22 '20 at 16:18
  • First of all, it would only translate the header, that is very light if you don't have the cpp included at the end. Second, it actually only includes it once, because of the header guards. You should include them in your hpp examples, btw. – Miguel Aug 22 '20 at 16:21
  • But main.cpp and prog.cpp are 2 different translation units, aren't them? So the header would be included in both – DarkoNaito_09 Aug 22 '20 at 16:36
  • in any case, only the header and not the whole source code. In fact, this is what will prevent dependencies problems – Miguel Aug 23 '20 at 12:58
0

The first version is what the book "Accelerated C++" (ed. 2000) suggests to do when first introducing header files for class declarations.

I suppose the second version can be seen as more elegant because it groups together all #include pre-compiler statements at the beginning of the program.

Giogre
  • 1,444
  • 7
  • 19