C++ headers and source files | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

13th Jun 2017, 10:17 AM
Deepesh Choudhary
Deepesh Choudhary - avatar
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.
13th Jun 2017, 10:27 AM
Karl T.
Karl T. - avatar
+ 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.
13th Jun 2017, 12:25 PM
Hatsy Rei
Hatsy Rei - avatar