01 / 21
#include <iostream> int main()

#include &
Declarations vs Definitions

Master the C++ preprocessor directive that brings code together, and learn the crucial difference between declaring and defining your functions and variables.

C++

What is #include?

Before your C++ code is compiled, a special tool called the preprocessor runs first. The #include directive tells the preprocessor: "Copy the entire contents of the specified file and paste them right here."

This is how your program gains access to library features like the output stream cout, the math function \(\text{sqrt}()\), and the string type: their declarations live in header files that you include.

Two Forms of #include

<angle brackets>
Searches the system/standard include paths. Used for standard library headers like <iostream>, <cmath>, <vector>.
"double quotes"
Searches your project directory first, then falls back to system paths. Used for your own header files like "myUtils.h".
// Standard library header #include <iostream> // Your own project header #include "myUtils.h"

The Preprocessor Pipeline

main.cpp (your source file) Preprocessor #include resolved #define expanded Translation Unit (expanded source) <iostream> "myUtils.h" Header files copied in Compiler (creates .o file)

Declarations vs Definitions

This is one of the most important distinctions in C++. A declaration tells the compiler that something exists and what its type is. A definition actually creates it: allocating memory or providing the function body.

Declaration
"This thing exists somewhere, and here is its name and type." No memory is allocated, no body is provided.
Definition
"Here is the actual thing." Memory is allocated for variables, or the full body is provided for functions.

The One Definition Rule (ODR)

C++ enforces a strict rule: every function and global variable must have exactly one definition across the entire program. Multiple definitions cause a linker error.

Declarations Can appear many times N times ✓ In headers, in .cpp files, across translation units vs Definitions Must appear exactly once 1 time only In one .cpp file (not in headers)

Functions: Declaration vs Definition

// DECLARATION (also called a "prototype") // Tells the compiler: "this function exists" int add(int a, int b); // ← semicolon, no body // DEFINITION (provides the actual body) int add(int a, int b) { return a + b; // ← curly braces with code }

Variables: Declaration vs Definition

// DECLARATION only (using extern) // "This variable exists somewhere else" extern int count; // ← no memory allocated here // DEFINITION (creates the variable) int count = 0; // ← memory allocated, value set // This is ALSO a definition (even without = 0) int count; // ← memory still allocated

Declaration vs Definition: Side by Side

Aspect Declaration Definition
Purpose Introduces name and type Creates the actual entity
Memory No allocation Allocates memory
Repetition Many times OK Exactly once (ODR)
Functions Ends with ; Has { body }
Variables Uses extern No extern, or has initializer
Where? Header files (.h) Source files (.cpp)

What Goes in a Header File?

✓ Put in headers
Function declarations (prototypes), class declarations, extern variable declarations, type aliases, constants
✕ Keep out of headers
Function definitions (bodies), global variable definitions, executable code that allocates memory
// myMath.h — header file (declarations only) #ifndef MYMATH_H #define MYMATH_H int add(int a, int b); // declaration double average(double*, int); // declaration #endif

How to Organize Your Code

1
Create a header file (.h) with function declarations and include guards.
2
Create a source file (.cpp) that includes the header and provides function definitions.
3
In your main file, use #include "myHeader.h" to access the declarations.
4
Compile all .cpp files together: the linker connects declarations to their matching definitions.

Example: Single File Program

In a small, single-file program, you often see both the declaration (prototype) and definition in the same file:

#include <iostream> #include <string> using namespace std; // Declaration (prototype) — tells compiler about greet void greet(string name); int main() { greet("Alice"); // OK: compiler knows greet exists return 0; } // Definition — provides the body void greet(string name) { cout << "Hello, " << name << endl; }

Example: Multi-File Project

math.h
#ifndef MATH_H
#define MATH_H
int square(int n);
#endif
math.cpp
#include "math.h"
int square(int n) {
  return n * n;
}
main.cpp
#include "math.h"
int main() {
  int x = square(5);
}

Include Guards: Preventing Double Inclusion

If file A includes file B, and file C also includes file B, then any file that includes both A and C gets B's contents twice. Include guards prevent this.

Traditional Guard
#ifndef MY_H
#define MY_H
... declarations ...
#endif
Modern Alternative
#pragma once
... declarations ...

Simpler, widely supported

Example: What Goes Wrong?

// BAD: putting a definition in a header // utils.h int helper() { // ← DEFINITION in header! return 42; } // a.cpp #include "utils.h" // gets one copy of helper() // b.cpp #include "utils.h" // gets ANOTHER copy of helper() // LINKER ERROR: multiple definition of `helper()`

Common Mistakes

✕ Defining in Headers
Placing function bodies in .h files causes multiple definition linker errors when the header is included by more than one source file.
✕ Forgetting Include Guards
Without #ifndef or #pragma once, a header can be pasted multiple times in one translation unit, causing redeclaration errors.
✕ Mixing Up <> and ""
Using <myFile.h> for your own files may fail because the compiler only searches system paths. Use "myFile.h" for project files.

Question 1 of 5

What does the #include directive do?

Question 2 of 5

Which of the following is a declaration but not a definition?

Question 3 of 5

You have two .cpp files that both include a header containing int helper() { return 42; }. What happens?

Question 4 of 5

What is the purpose of include guards (#ifndef / #define / #endif)?

Question 5 of 5

Which #include syntax should you use for your own project header file called player.h?