The best way to think of the header file is it is a template or definition of the class with their methods, structs and functions.
When you include a header using #include
the compiler is then made aware of the definitions and takes them as correct. This method hasn’t really changed since the days of K&R C from the 1970s which required you to define your functions before they are used or forward-declare them. This is why the headers don’t generate the errors when the calls to the methods or functions don’t match the definition in the headers - it’s the actual CPP file that has the error. The headers themselves actually facilitate the use of multiple files for your code so when you need to identify available methods/functions to the code file, you don’t have to forward declare each method you use, just include.
For languages that don’t take this approach, code has to be searched for the function to ensure the calls match the declaration and so it takes a lot longer to compile code.
So, C/C++ uses #include
to ensure when a function/method is used, the parameters match what is expected of it.