I’m confused about what goes in the header file and what goes in the c file, and why not just define the functions in the header file?, I’ve searched for answers but I find it a little complicated to understand, share your thoughts!
Think of the Header file as the Promise… and the cpp file as the fulfillment of that promise.
It’s a throwback to the early days of C. Instead of having to reread a cpp file hundreds of times in a large project (remember, every cpp file references things like the std:: primitives…), the compiler only has to read the actual implementations (definitions) once. After that, it only really needs the declaration, the address header, so to speak…
Another reason is that in many libraries, the actual source code is not released… you get the .H file and a compiled file. The .H is public, like the Phone Directory.
Here’s an abstract way of looking at it:
Anybody can see that you live at 4 Privet Drive, the Cupboard Under the Stairs, and if I want to write a letter to that address, that’s all I really need to know. That’s the declaration in the Header File. What I don’t need to know is the exact layout of 4 Privet Drive, in order to get the letter there. When the letter arrives, the occupants of the house know what to do to get it to you. That’s the implementation or definition.
Thank you very much, that’s very simple.
That has got to be one of the geekiest and coolest ways I’ve read header files explained.