3.1.3 Header and Implementation File
While defining functions inside of a header file can be common for a small library, most libraries are implemented by using a combination of a header file and an implementation file. The header file generally contains function prototypes and import statements needed for those function headers and the implementation file then carries the function definitions. This split is generally done so that a developer can look at the header file to understand how to use a library, but doesn’t need to get into all the details of how the functions are implemented.
Creating the Implementation File
By convention, implementation files have the same root name as the header file, but carry a C++ file extension, .cpp
. Logistically, the implementation file needs to import the header file and any additional import statements needed for the function definition that were not imported in the header file. An import that was done in the header file does not need to be included in the implementation file.
Input Library
Let’s take a look back at our input library from the last example.
Original Header File:
To use this as a header/implementation setup, you will split this into two files:
Notice how the header file is now more streamlined and provides information that is needed for a developer to use the library. The implementation file now separates out the details for how the function works.
The implementation file needs to include the input header file, but doesn’t need to include the iostream
again.
To use the library, you only need to include the header file, just like before.
Last updated