Declarations (indicating that something exists) that should be seen in more than one cpp file should go into the header files. Declarations that are local to a single cpp file must be in the cpp file itself.
Definitions (providing body functions or variable allocation / initialization) should usually be included in cpp files, but not always.
The question you need to understand is whether the compiler has enough information to do its job if it saw a header file and not the corresponding cpp file.
For example: you can call a method if the compiler saw the declaration (prototype of the method) - if only this method is not general (the template method or a member of the template class) or built-in, in this case the compiler is needed to see the definition (method body) too.
Therefore, the usual methods go to cpp files; template methods go to header files; built-in methods go to header files (etc.).
There are other situations in which definitions apply to header files, including static member constants. All this returns to providing the compiler with the information he needs with one hand, and minimizing the connection between the individual compilation units on the other. Again there are no hard and fast rules, just guidelines combined with the knowledge and experience of the developer who wrote the code.
Dale wilson
source share