← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 6 of 17

C++ Header Files

A header file is a separate file that lists what code offers, like a table of contents. Other files include it to use that code.
Syntax
cpp
// filename.h
#ifndef FILENAME_H
#define FILENAME_H

// declarations

#endif

// filename.cpp
#include "filename.h"

Why Use Header Files?

Header files are used to separate declarations from implementations. They contain function declarations, class definitions, and macro definitions. This allows you to share declarations across multiple source files.

Example: Why Use Header Files?

cpp
// shape.h
class Shape {
public:
	double area();
};

// shape.cpp
#include "shape.h"
double Shape::area() { return 0.0; }

Header Guards

Header guards prevent a header file from being included multiple times in the same file. This avoids duplicate definition compiler errors.

Example: Header Guards

cpp
// shape.h
#ifndef SHAPE_H
#define SHAPE_H

class Shape {
public:
	double area();
};

#endif

System Headers vs User Headers

System headers are included using angle brackets, like . This tells the compiler to search the system directories first.

Local user headers are included using double quotes, like "MyHeader.h", which tells the compiler to search your project folder first.

Example: System Headers vs User Headers

cpp
// Include std::cout and std::cin
#include <iostream>
#include "myheader.h"

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Angle brackets vs quotes" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Declaring Classes in Headers

To organize your code, define class blueprints in a header file and implement the actual member functions in a corresponding source file.

Example: Declaring Classes in Headers

cpp
// point.h
class Point {
public:
	int x, y;
	double distanceFromOrigin();
};

// point.cpp
#include "point.h"
#include <cmath>
double Point::distanceFromOrigin() {
	return std::sqrt(x * x + y * y);
}

Inline Functions in Headers

If you define a function directly inside a header, declare it as inline. This tells the linker to allow multiple definitions of the function across different source files without causing conflict errors.

Example: Inline Functions in Headers

cpp
// mathutils.h
inline int square(int x) {
	return x * x; // "inline" avoids duplicate-definition linker errors
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting header guards, which causes redefinition errors.
  2. Putting function definitions in a header included by multiple files.
  3. Using angle brackets for your own headers instead of quotes.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.