Separate Compilation
- Real programs are composed of multiple files
- Files are compiled separately into object files
- The object files are linked together using the linker
- For each application, one file is the main program and contains the main function
- Separating functions and classes into separate files increases cohesion (good) but also increases coupling
File Structure
- Include file: *.hpp includes function declarations, e.g., Aggregate.hpp
- Implementation file: *.cpp includes function definitions, e.g., Aggregate.cpp
- Two files are highly related
Include File
- Required header comment, as in any other source file
- Required include guard to prevent the compiler from seeing these contents more than once
- Insert only the include of any files needed for the function declarations
- Insert the function declarations (function prototypes)
- The code that uses the include file must compile no matter where it is included or which includes precede it
Implementation File
- Required header comment, as in any other source file
- Include the related include file first, then when you compile the implementation file, you check that the include file is complete.
- Insert any included files needed for the implementation
- An include file is needed in the implementation file if it is used in the implementation of the code, i.e., the function definition
- Do not include .cpp files
Roles
- Include files (*.hpp) contain the interface concerns
- Implementation files (*.cpp) contain the implementation concerns
- Hide as many concerns as possible in the implementation files
Self-Sufficiency
- An include file needs to be self-sufficient
- It should not depend on previous includes or the order of includes
- This applies to all include files that we write
Exposing Implementation Concerns
Hiding Implementation Concerns