Why are declarations and implementations separated in .h and .cpp?

Benefit 1: Clarity and Readability

The first and foremost benefit of placing declarations and implementations separately is that it allows others reading the code you write to quickly understand what functionality and external interfaces your program provides, without being buried in complex implementation details.

Separate placement is consistent with the idea of encapsulation in facial object programming - the interface caller only needs to know how to use, without having to pay attention to the specific details of the implementation.

Benefit 2: Avoid writing a lot of declarations over and over again

By centralizing function declarations in the header file, you can avoid writing the same declarations repeatedly in multiple source files. This only needs to be declared once in the header file, other files through #include the introduction of the header file can reuse the declaration, reducing redundant code.

Benefit 3: Improve compilation efficiency

Modifying the contents of a header file will cause all source code files (.cpp) that #include that header file to be recompiled.

If the implementation of a function is placed in the header file, it can easily trigger frequent compilation operations, which can be a huge waste of time in large projects.

Therefore, only the declaration part of the function is placed in the imported header file, while the implementation part of the function is placed in a separate implementation file. In this way, if only the implementation details of a function are modified without changing the function's external interface, only the corresponding implementation file will be recompiled, while other files relying on the header file will not be affected, thus improving compilation efficiency.

Benefit 4 (the most critical): Avoid redefinition errors at runtime

C++ is a compiled programming language. A C++ program can contain multiple source code files, each of which is compiled by the compiler into a target file, and finally assembled together by the linker to produce an executable file.

#include is the equivalent of copying the contents of the included file to that line.

If you write the implementation of a function in a header file, this can lead to problems with duplicate definitions.

Specifically, when you introduce .h files containing the implementation in different .cpp files, then the function you wrote in .h will be copied twice, and during the linking phase, the compiler will notice that there are more than one of the same function definitions, and report a duplicate definition error.


I'm a junior undergraduate majoring in Computer Science and Technology, and I'm always reading technical articles written by others.

Finally, today I was determined to write something myself, and for less than 700 words, I wrote and deleted and deleted and wrote again, taking more than 2 hours in total...

But it was a great big step for me!

I'll be writing more and more technical articles next! Documenting all the problems I've encountered and the knowledge I've learned will hopefully help those rookie programmers like me on their way to growth.