+ 1
C++ headers and source files
I saw that function prototypes and class declarations are in header files, and their definitions in .cpp files. 1) Why so? 2) Whatif I define them in the header itself? 3) Any advantages of this?
2 Answers
+ 7
1) This is a relic of C, when compilers needed header files for single pass compilation.
2) You can, the methods will then be inline. They will be quicker to execute but take more space in the executable.
3) Advantage is that your code is split into modules, making the headers like some form of documentation of your code.
+ 7
Btw, there is a little problem when you deal with ADT classes and splitting them into .h and .cpp. There is a workaround for this by specifying one more line into the .cpp file, but let's deal with that when the proper question arises.