01 / 20
.h .cpp

Organizing Header
& Source Files

Learn how to split your C++ code across .h/.hpp and .cpp files for clean, scalable, and error-free projects.

C++

What Are Header and Source Files?

In C++, code is organized into two types of files that work together. Each plays a distinct role in the compilation process:

.h / .hpp : Header File
Contains declarations: function prototypes, class definitions, constants, and type aliases. Acts as the interface others can see.
.cpp : Source File
Contains implementations: function bodies, method definitions, and executable logic. Acts as the hidden machinery.

Why Separate Into Multiple Files?

1
Faster compilation: Only modified .cpp files get recompiled. Unchanged files are skipped.
2
Team collaboration: Multiple programmers can work on different .cpp files simultaneously without conflicts.
3
Code reuse: Any file can #include a header to use its declarations without duplicating code.
4
Readability: Separating interface from implementation makes large projects navigable and maintainable.

The Compilation Pipeline

Preprocessor #include expands Compiler .cpp → .o Linker .o files → exe a.out math.h math.cpp main.cpp math.o main.o program

The One Definition Rule (ODR)

C++ enforces a strict rule: every function and variable may be defined (given a body or value) in exactly one translation unit. But it can be declared (announced to exist) in many units.

Declaration
"This function exists." Tells the compiler about a name and its type. No body, no storage.
Definition
"Here is the function." Provides the actual body or allocates storage. Must appear only once.

Declaration vs Definition

Declaration Definition
Purpose Announces a name exists Provides the body or value
Where .h / .hpp .cpp
Can repeat? Yes (in many files) No (exactly once)
Function example int add(int, int); int add(int a, int b) { return a+b; }
Variable example extern int count; int count = 0;

Include Guards Prevent Duplication

If main.cpp includes a.h and b.h, and b.h also includes a.h, the preprocessor will paste a.h contents twice. Include guards stop this:

#ifndef / #define
Traditional macro guard. Wrap the entire header in a unique #ifndef HEADER_NAME_H block.
#pragma once
Modern compiler directive. One line at the top. Not part of the standard, but supported by all major compilers.
math_utils.h (with guard)
#ifndef MATH_UTILS_H #define MATH_UTILS_H int add(int a, int b); int multiply(int a, int b); #endif // MATH_UTILS_H

How to Organize Your Files

1
Create a header file (.h) with an include guard and all declarations: function prototypes, class definitions, constants.
2
Create a matching source file (.cpp) that #includes its own header and implements every declared function.
3
In main.cpp, include only the headers you need. Never include .cpp files.
4
Compile separately, link together: g++ -c math.cpp, g++ -c main.cpp, then g++ math.o main.o -o program.

Example 1: Simple Function Split

greet.h
#pragma once #include <string> std::string greet(const std::string& name);
greet.cpp
#include "greet.h" std::string greet(const std::string& name) { return "Hello, " + name + "!"; }

Example 2: Class Across Files

Rectangle.h
#pragma once class Rectangle { public: Rectangle(double w, double h); double area() const; double perimeter() const; private: double width, height; };
Rectangle.cpp
#include "Rectangle.h" Rectangle::Rectangle(double w, double h) : width(w), height(h) {} double Rectangle::area() const { return width * height; } double Rectangle::perimeter() const { return 2 * (width + height); }

Example 2: Using the Class

main.cpp
#include <iostream> #include "Rectangle.h" int main() { Rectangle r(5.0, 3.0); std::cout << "Area: " << r.area() << "\n"; std::cout << "Perimeter: " << r.perimeter() << "\n"; return 0; }

Example 3: Multi-File Project Structure

my_project/ main.cpp math/ math.h math.cpp utils/ utils.h utils.cpp .h header (interface) .cpp source (implementation) Include relationships: main.cpp includes math.h and utils.h math.cpp includes math.h | utils.cpp includes utils.h

What Goes Where?

Element Header (.h) Source (.cpp)
Function prototype ✓
Function body ✓
Class definition ✓
Method bodies ✓
Constants (constexpr) ✓
Global variable extern only ✓ definition
Inline functions ✓
#include guards ✓ Not needed

.h vs .hpp: What's the Difference?

Both extensions serve the same purpose. The distinction is purely a convention some teams adopt:

.h
The traditional extension. Used by most C++ projects and all C projects. Universal and well-understood.
.hpp
Signals "this is a C++-only header." Helps distinguish from C headers in mixed C/C++ codebases.

Common Mistakes to Avoid

✕ Defining functions in headers
Putting full function bodies in .h files causes "multiple definition" linker errors when included by two or more .cpp files.
✕ Including .cpp files
Writing #include "math.cpp" pastes the entire implementation, causing duplicate definitions. Only include headers.
✕ Missing include guards
Without guards, the same header gets pasted multiple times, causing "redefinition" compiler errors for classes and structs.
✕ Circular includes
a.h includes b.h which includes a.h. Use forward declarations to break the cycle.

Question 1 of 5

Which of the following should not be placed in a header file?

Question 2 of 5

What is the primary purpose of an include guard?

Question 3 of 5

You have main.cpp that uses functions from utils.h. Which compile command sequence is correct?

Question 4 of 5

A student writes #include "math.cpp" in their main file. What is the most likely result?

Question 5 of 5

In a Rectangle class split across files, where should the constructor body Rectangle::Rectangle(double w, double h) : width(w), height(h) {} be written?