Free Handbook · Every example compiled & verified

C++ Handbook

Learn C++ the way it is written at work. Seventeen modules from your first program to a job: every example program was compiled with clang as C++20 and run before publishing, with its output shown exactly as it printed, and the real compiler, linker and sanitizer errors you will hit are explained with the fix. Types and initialization, flow control, functions and lambdas, arrays, vectors and strings, pointers and references, memory and smart pointers, classes, move semantics, inheritance and polymorphism, templates and the STL, modern C++ (optional, variant, span, constexpr, std::format), debugging with sanitizers, C++ with CMake, Conan, GoogleTest, clang-tidy, PostgreSQL, Qt, Docker and GitHub Actions, data structures and algorithms, problem-solving patterns, interview questions, a certification exam, and a resume check at the end.

0 / 145 lessons🔥 0 day streak
ShareXLinkedIn

How this handbook works

Every lesson has a complete C++ program with the exact output it prints — each one compiled and run with the real C++ toolchain before it was published, so what you see is what you will get when you run it yourself — a trace of what the program did line by line where that matters, and the real compiler or runtime error you will hit next, with why it happened and the fix. Lessons tick themselves as you go. Finish the handbook, sit the exam, get the certificate, then take your resume through the ATS checker against a real job posting.

17modules
145lessons
126compiled examples
73real errors explained

Module 00 · what you'll be able to do

  • Explain where C++ is used today and what kind of jobs ask for it
  • Install g++, clang or MSVC on macOS, Windows or Linux and check it works
  • Compile and run a program with g++ -std=c++20 main.cpp -o main
  • Describe the four build stages — preprocess, compile, assemble, link — and tell a compiler error from a linker error
  • Read the structure of a C++ program: includes, main, statements, namespaces and comments
01

Why learn C++

C++ is a compiled, statically typed language that gives you direct control over memory and hardware while still offering high-level tools: classes, templates, a large standard library and automatic cleanup through destructors. That combination — fast like C, expressive like a modern language — is why it still runs the software where speed and predictability matter most.

Bjarne Stroustrup started it in 1979 as "C with Classes" at Bell Labs; it was renamed C++ in 1983 and has been an ISO standard since 1998. The language now evolves on a three-year cycle, and this handbook teaches C++20, with notes where C++17 (still common in industry) differs.

Where C++ runsExamplesWhy C++ there
Games and enginesUnreal Engine, most AAA console gamesFrame budgets of 16 ms leave no room for a garbage collector pause
BrowsersChrome, Firefox, Safari enginesHuge codebases that must be fast and run on every platform
Databases and infrastructureMySQL, MongoDB, ClickHouse, RocksDBTight control over memory layout, caching and I/O
FinanceLow-latency trading systemsMicroseconds are money; predictable performance
Embedded and automotiveCar ECUs, drones, medical devicesRuns on small chips with no operating system at all
AI and scientific computingThe engines under PyTorch and TensorFlow, CUDA kernelsPython calls into C++ for the heavy maths
Compilers and toolsLLVM/clang, GCC, the JVM and V8 runtimesSystems that other languages are built on
In real jobs
C++ roles are rarely titled just "C++ developer". Look for systems engineer, game programmer, embedded software engineer, low-latency developer, graphics engineer and performance engineer. They pay well because the skill is harder to fake: interviewers will ask about memory, pointers and object lifetimes, which Modules 05–07 cover in depth.

C++ is a good first language if…

  • You want to understand how computers really work
  • You are aiming at games, embedded, finance or systems work
  • You like knowing exactly what your code costs

Consider starting elsewhere if…

  • You want a web app running this week (try JavaScript)
  • You want data analysis quickly (try Python)
  • You find compile errors discouraging — though this handbook explains every one it shows
02

Install a C++ compiler

C++ source code is plain text. A compiler turns it into a native executable for your machine. There are three major compilers, all free: GCC (the g++ command, standard on Linux), Clang (clang++, standard on macOS) and MSVC (Microsoft's, used by Visual Studio on Windows). Any of them runs every example in this handbook.

macOS

bash
# Installs Apple clang, make, git and the SDK headers (no full Xcode needed)
xcode-select --install

# Check it worked — on macOS "g++" is an alias for clang
clang++ --version
g++ --version

On a Mac, g++, c++ and clang++ all run Apple clang. If you specifically want GCC, brew install gcc installs it as g++-14 (the number is the version).

Windows

bash
# Option A — MSVC: install "Visual Studio Community" and tick
#   "Desktop development with C++". Then open
#   "Developer Command Prompt for VS" and compile with:
cl /std:c++20 /EHsc main.cpp

# Option B — GCC via MSYS2 (gives you g++ in a normal terminal)
winget install MSYS2.MSYS2
# then, inside the "MSYS2 UCRT64" shell:
pacman -S mingw-w64-ucrt-x86_64-gcc
g++ --version

MSVC is what most Windows game studios use. MSYS2 gives you the same g++ commands this handbook shows. If you use WSL (Windows Subsystem for Linux), follow the Linux steps inside it.

Linux

bash
# Debian / Ubuntu
sudo apt update && sudo apt install build-essential

# Fedora
sudo dnf install gcc-c++

# Arch
sudo pacman -S gcc

g++ --version

build-essential installs g++, make and the C/C++ standard library headers. GCC 11 or newer supports everything in this handbook with -std=c++20.

Which editor?
VS Code with the clangd extension (errors as you type, go-to-definition) is the lightest setup. CLion is a full C++ IDE, free for non-commercial use. Visual Studio Community is the standard on Windows. The compiler is what matters — the editor just calls it.
03

Your first C++ program

Create a folder, save the program below as main.cpp, then compile and run it. Every example in this handbook is a complete program like this one, and the output shown beneath it is exactly what it prints.

C++main.cpp
#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    std::cout << "2 + 3 = " << 2 + 3 << '\n';
    return 0;
}
Outputcompiled & run with real C++
Hello, C++!
2 + 3 = 5
Your turn

Add a third line that prints your name. Then remove the semicolon after one statement, compile again and read the error — compiler messages are something you will read every day.

bash
g++ -std=c++20 main.cpp -o main   # compile: main.cpp -> an executable named "main"
./main                              # run it (on Windows: main.exe)

-std=c++20 picks the language version; -o main names the output file. Without -o you get a.out (a.exe on Windows).

VisualizeWhat happens when ./main runsStep 1 / 5
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
std::cout << "2 + 3 = " << 2 + 3 << '\n';
return 0;
}
Line 1

Before compiling, the preprocessor pastes in the <iostream> header, which declares std::cout.

Variables now

nothing yet

All 5 steps as a table
StepLineWhat happenedVariables now
11Before compiling, the preprocessor pastes in the <iostream> header, which declares std::cout.
23The operating system starts the program by calling main. Every C++ program has exactly one.
34<< sends the text to standard output. \n is a newline character.
452 + 3 is evaluated first (arithmetic binds tighter than <<), then each piece is streamed in order.
56Returning 0 from main tells the operating system the program succeeded. Any other value means failure.
Error you will hit

expected ';' after expression

C++
#include <iostream>

int main() {
    std::cout << "Hello, C++!\n"
    return 0;
}
main.cpp:4:33: error: expected ';' after expression
    4 |     std::cout << "Hello, C++!\n"
      |                                 ^
      |                                 ;
Why the compiler said that

Every statement in C++ ends with a semicolon. Line breaks mean nothing to the compiler, so it read line 4 and line 5 as one long statement and stopped when it found return where it expected ;. Notice it points at the end of line 4, the spot where the semicolon belongs.

The fix

Add the missing semicolon at the position the caret points to.

C++
#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    return 0;
}
05

How a C++ program is structured

Every program in this handbook follows the same skeleton. Here it is with each part labelled in comments. // starts a comment that runs to the end of the line; /* … */ can span several lines. Comments are removed before compiling and cost nothing at runtime.

C++main.cpp
// 1. Includes: pull in the declarations you need
#include <iostream>
#include <string>

/* 2. Your own functions go above main
      (or are declared above it and defined later). */
std::string greet(const std::string& name) {
    return "Hello, " + name;
}

// 3. main: where the program starts
int main() {
    std::string who = "world";      // a variable with a type
    std::cout << greet(who) << '\n';
    return 0;                       // 0 = success
}
Outputcompiled & run with real C++
Hello, world
Your turn

Add a second function shout that returns its argument followed by "!", and print shout(greet(who)).

PieceWhat it means
#include <iostream>Paste in the standard header that declares std::cout and std::cin. Angle brackets = standard or system header; quotes ("mine.h") = your own header.
std::Everything in the standard library lives in the namespace std. std::cout means "the cout that belongs to std".
int main()The entry point. Returns an int exit status. If you leave out return 0;, main (and only main) returns 0 automatically.
{ }A block: groups statements and defines a scope. Variables created inside disappear at the closing brace.
;Ends a statement. Indentation and line breaks are for humans only.
Why this handbook avoids "using namespace std;"
Many tutorials start with using namespace std; so they can write cout instead of std::cout. It works in a tiny file, but it dumps hundreds of names (count, distance, size, data…) into your program, where they clash with your own. Professional code bases forbid it in headers, and most forbid it everywhere. Writing std:: is five characters of clarity.
Error you will hit

use of undeclared identifier 'cout'

C++
#include <iostream>

int main() {
    cout << "Hello\n";
}
main.cpp:4:5: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
    4 |     cout << "Hello\n";
      |     ^~~~
      |     std::cout
Why the compiler said that

cout is not a global name; it lives inside namespace std. Clang even guesses what you meant and suggests std::cout.

The fix

Write the full name std::cout.

C++
#include <iostream>

int main() {
    std::cout << "Hello\n";
}
06

Compiler flags worth using from day one

The compiler can catch many bugs for you, but only if you ask. These flags work with both g++ and clang++; the MSVC equivalents are in the last column.

FlagWhat it doesMSVC
-std=c++20Use the C++20 language standard/std:c++20
-Wall -WextraTurn on the common warnings (unused variables, sign comparisons, missing returns…)/W4
-WerrorTreat every warning as an error, so none slip through/WX
-gInclude debug information so a debugger can show your source lines/Zi
-O2Optimise for speed (use for release builds; debug with -O0)/O2
-fsanitize=address,undefinedAdd runtime checks that catch memory errors and undefined behaviour with a clear report/fsanitize=address
bash
# A good everyday "learning" build command:
g++ -std=c++20 -Wall -Wextra -g -fsanitize=address,undefined main.cpp -o main

Slower to run, but most beginner bugs become a clear message instead of a silent wrong answer. Module 06 shows what the sanitizer reports look like.

Error you will hit

A warning that saves you a bug: comparison of integers of different signs

C++
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    for (int i = 0; i < v.size(); i++) {
    }
}
main.cpp:5:23: warning: comparison of integers of different signs: 'int' and 'size_type' (aka 'unsigned long') [-Wsign-compare]
    5 |     for (int i = 0; i < v.size(); i++) {
      |                     ~ ^ ~~~~~~~~
Why the compiler said that

v.size() returns an unsigned type. Comparing it with a signed int converts the int to unsigned first, so a negative number would become a huge positive one. This is only a warning — the program still builds — but it is exactly the kind of bug Module 01 explains. Without -Wall -Wextra you would never see it.

The fix

Use an unsigned index type (std::size_t), or better, a range-based for loop that has no index at all (Module 02).

C++
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    for (std::size_t i = 0; i < v.size(); i++) {
    }
}
07

How this handbook works

C++ cannot run inside a web page, so this handbook does the next best thing: every example was compiled with a real C++20 compiler and run, and the output printed under it is exactly what came out. A script re-checks every example whenever the handbook changes, so the page never drifts from what the code really does.

  • Copy, compile, change. Paste an example into main.cpp, build it with g++ -std=c++20 main.cpp -o main, then do the "Your turn" task. Reading code teaches you little; changing it and predicting the result teaches you a lot.
  • Error cards show real compiler and sanitizer messages, copied from actually building the code, with why it happened and the fix. Learn to read the first line of an error: file:line:column: error: message.
  • Traces step through a program line by line, showing variables and output as they change.
  • The floating board collects every term from the glossary blocks, so you can look up "RAII" or "dangling pointer" without losing your place.
  • The certification exam at the end: 25 questions drawn from all modules, pass with 70% to earn a free certificate.
Using AI to learn without cheating yourself
AI assistants are good at C++ and good at explaining errors. Use them to explain a message you do not understand, or to review code you already wrote. Do not ask them to write the "Your turn" exercises — the point of an exercise is the struggle, and interviews (Module 15) will ask you to write C++ without help. A simple rule: attempt it for fifteen minutes first, then ask for a hint, not an answer.
The path through the handbook
Modules 01–04 are the core language (types, control flow, functions, arrays and strings). Modules 05–07 are what makes C++ different: pointers, memory ownership and classes. Modules 08–10 cover inheritance, templates and the STL, and modern C++. Then errors and debugging, tools, data structures, problem solving, interview questions and getting hired.

Every lesson in the handbook

17 modules · 145 lessons · each one ticks itself when you reach the end.

00Getting Started0 / 7
01Fundamentals0 / 7
02Flow Control0 / 8
03Functions0 / 8
04Arrays & Strings0 / 8
05Pointers & References0 / 8
06Memory & Smart Pointers0 / 8
07Classes & Objects0 / 7
08Inheritance & Polymorphism0 / 8
09Templates & the STL0 / 8
10Modern C++0 / 8
11Errors & Debugging0 / 8
12C++ + Tools0 / 10
13Data Structures & Algorithms0 / 14
14Problem Solving0 / 12
15Interview Questions0 / 10
16Job Ready0 / 6

Frequently asked questions

Is this C++ handbook free?
Yes — every module, every example and the certification exam are free, with no signup and no paywall. The exam asks you to sign in with Google so the certificate can carry your name.
Do I need to know another programming language first?
No. Module 01 starts from variables and types and assumes nothing, although C++ is easier if you have written some code before. The handbook explains the ideas other languages hide from you: the stack and the heap, pointers and references, object lifetimes, copying versus moving, and undefined behaviour.
Do I need to install anything?
Yes — a C++ compiler (clang, gcc or Visual Studio), and Module 00 shows how to install one on macOS, Windows and Linux and build your first program. The examples on this page do not run in the browser; instead, each example program was compiled with clang as C++20 and run before publishing, and the output shown is exactly what it printed, so you can paste it into main.cpp, compile it with c++ -std=c++20 main.cpp -o prog and get the same result. The labs in Module 12 need libraries and tools such as CMake, Conan, Qt and PostgreSQL, so they are shown as clearly marked snippets.
Which version of C++ does this handbook teach?
Modern C++, compiled as C++20: smart pointers, move semantics, lambdas, auto, structured bindings, std::optional, std::variant, std::span, constexpr, concepts, ranges and std::format. Where a feature needs C++17 or C++20 the lesson says so, and older styles you will meet in existing code (raw new and delete, C arrays and strings) are explained so you can read and replace them.
Is there a certificate?
Yes. Pass the 25-question exam (35 minutes) with 70% or more and generate a SolutionGigs C++ certificate with your name, score, date and a certificate ID — download it, print it or add it to LinkedIn.
How long does it take to learn C++?
Most people can write useful small programs after the first five modules — a few weeks of steady practice. Being job-ready takes longer, typically several months, because employers expect you to understand memory, object lifetimes and the standard library well (Modules 05–10).
Should I learn C before C++?
No. Modern C++ teaches safer habits than C: std::string instead of character arrays, std::vector instead of manual arrays, smart pointers instead of malloc/free. You will still learn raw pointers and C-style arrays here, because real code bases use them.
Is g++ or clang better for learning?
Either. They accept the same code and the same flags. Clang's error messages are often a little friendlier, which is why the error cards in this handbook use them; GCC's say the same thing in different words.

Finish the C++ handbook, then get hired

Sit the exam for your certificate, run your resume through the ATS checker, and see the jobs that ask for exactly this.

Check my resume
Found this course useful? Share it.
ShareXLinkedIn

Comments

0

Join the conversation. Sign in to leave a comment — we'd love to hear your thoughts.