C++20 sections · 1024 units
Open in Course

Header Files Basics

Reusing across files

Header files hold declarations. Put function signatures in a .h file, include it wherever needed. This shares functions across multiple source files without rewriting declarations.

Example: create math.h with int add(int a, int b);. In math.cpp, include and define it. In main.cpp, include and call add(5, 3). Use include guards: #ifndef MATH_H, #define MATH_H, #endif.

If included twice, the guard skips the second. Prevents redefinition errors.