When dividing your code up into multiple files, what exactly should go into an .h file and what should go into a .cpp file?
When dividing your code up into multiple files, what exactly should go into an
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.cppfiles. 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.