After tutoring a few students at a local college, I was asked “when is best to use a header file in C++ compared to a regular .cpp file?”. I kind of struggled with the answer and was seeing if there is a more definite answer of what is best under which scenarios.
Is a header file best used when using code that can be used in multiple projects? I know this is a stupid example but if you made custom math functions that could be used over and over in other projects also; that would be best placed in a header file correct?
Anything that needs to be accessed from more than one
.cppfile should get declared in a header file, and the header file should be#includedinto each relevant.cppfile.This can, for example, include declaration of global variables, global functions, classes etc.