Learn C++ – tutorial, cheatsheet & exercises
C++ is a compiled, statically typed language that offers maximum control over memory and performance – without giving up abstractions. It began as "C with Classes" and is now a multi-paradigm language: procedural, object-oriented, generic (templates) and increasingly functional as well (lambdas, ranges).
You find C++ wherever speed and resource control matter: games and game engines (Unreal), browsers (Chrome, Firefox), databases, financial and trading systems, embedded software, simulations and parts of operating systems. Modern C++ (C++11 to C++23) differs greatly from the old "C with new/delete": you work with RAII, smart pointers, STL containers and algorithms, and thereby write safe, readable code.
The downside: C++ does not forgive mistakes. A wrong access is not a clean error but undefined behavior. This learning site therefore emphasizes good habits: const correctness, references, the standard library instead of manual work, and compiler warnings as a friend.
Hello World in C++
#include <iostream>
int main() {
std::cout << "Hello World!\n";
}std::cout writes to the console, main is the entry point. Compile with g++ -std=c++20 hallo.cpp -o hallo.
How to get started with C++
You need a C++ compiler. The two important ones are g++ (GCC) and clang++. Both fully support C++17 and largely support C++20 – use a recent version (GCC 12+ or Clang 15+; for std::format GCC 13+ or Clang 17+).
Installation
# macOS: Apple Clang including command line tools
xcode-select --install
# Debian/Ubuntu (or WSL on Windows)
sudo apt install g++ clang cmake gdb
# Windows (MSYS2, then in the UCRT64 terminal)
pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-cmakeOn Windows you have three good options: MSYS2/MinGW (g++ as on Linux), Visual Studio with the "Desktop development with C++" workload (compiler cl) or WSL (a real Linux, recommended for getting started). Afterwards, check the installation:
g++ --version
clang++ --version # if available (on the Mac, g++ is usually an alias for clang++)Compiling and running
C++ is translated into a program before it runs. With -std=c++20 you choose the language standard, and -Wall -Wextra enables useful warnings:
g++ -std=c++20 -Wall -Wextra -O2 hallo.cpp -o hallo
./hallo # on Windows: hallo.exeFor multiple files and larger projects, use CMake. A minimal CMakeLists.txt next to your main.cpp:
cmake_minimum_required(VERSION 3.16)
project(MeinProjekt LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(meinprogramm main.cpp)
target_compile_options(meinprogramm PRIVATE -Wall -Wextra)cmake -S . -B build
cmake --build build
./build/meinprogrammg++ -std=c++20 -g -fsanitize=address,undefined main.cpp reports memory errors and undefined behavior at runtime. For threads, add -pthread.C++ learning path
Hello World, main and #include
Structure of a C++ program, output with std::cout and the path from source code to program.
Variables, Types and const
Basic types, type inference with auto, safe initialization and constants.
Conditions: if, switch and Operators
Making decisions with if/else, switch, logical operators and the ternary operator.
Loops: for, range-for and while
Repeating code – over numbers and containers, with break and continue.
Functions, Overloading and Declarations
Defining functions, default arguments, overloading and multiple return values.
References, Pointers and const Correctness
Passing by value, by reference and by const reference, plus the basics of pointers.
std::string and std::vector
The two most important standard types: text and dynamic arrays.
Input, Files and Streams
Reading from the console, writing and reading files, splitting text with stringstream.
Classes, Constructors and Destructors
Your own types with data and behavior – including const methods and operators.
Inheritance, virtual and Polymorphism
Base classes, virtual methods, abstract classes and the slicing trap.
RAII and Smart Pointers
Managing resources safely with unique_ptr, shared_ptr and weak_ptr – without new and delete.
STL: Containers, Algorithms and Lambdas
map, set, sort, find, transform and lambdas – the toolbox of the standard library.
Error Handling: Exceptions and optional
try/catch, custom exception classes, noexcept and alternatives to exceptions.
Templates and Generic Programming
Function and class templates, variadic templates and fold expressions.
Move Semantics and the Rule of Zero/Five
Moving resources instead of copying, rvalue references and perfect forwarding.
Ranges and Concepts (C++20)
Readable data pipelines with views and templates with clear requirements.
Undefined Behavior and Typical Pitfalls
Dangling references, iterator invalidation, overflows – and how sanitizers find them.
Modern C++: optional, variant, string_view and constexpr
Expressive building blocks from C++17/20 for safer and faster code.
At a glance
More about C++
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: