What should go into an .h file?

Enrico Tuvera Jr picture Enrico Tuvera Jr · Dec 22, 2009 · Viewed 87.8k times · Source

When dividing your code up into multiple files just what exactly should go into an .h file and what should go into a .cpp file?

Answer

Amber picture Amber · Dec 22, 2009

Header files (.h) are designed to provide the information that will be needed in multiple files. Things like class declarations, function prototypes, and enumerations typically go in header files. In a word, "definitions".

Code files (.cpp) are designed to provide the implementation information that only needs to be known in one file. In general, function bodies, and internal variables that should/will never be accessed by other modules, are what belong in .cpp files. In a word, "implementations".

The simplest question to ask yourself to determine what belongs where is "if I change this, will I have to change code in other files to make things compile again?" If the answer is "yes" it probably belongs in the header file; if the answer is "no" it probably belongs in the code file.