C++C++

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.

GamesGame enginesFinancial systemsBrowsersEmbedded

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-cmake

On 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.exe

For 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/meinprogramm
TipWhen learning and debugging, sanitizers are worth it: g++ -std=c++20 -g -fsanitize=address,undefined main.cpp reports memory errors and undefined behavior at runtime. For threads, add -pthread.

C++ learning path

Beginner
Intermediate
Advanced

At a glance

First released1985
CreatorBjarne Stroustrup
ParadigmMulti-paradigm (OOP, generic, procedural)
TypingStatic, strong
ExecutionCompiled to machine code
File extension.cpp / .hpp

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

C++ compared

Hello World: C++ comparedVariables: C++ comparedConditionals: C++ comparedLoops: C++ comparedFunctions: C++ comparedLists and Arrays: C++ comparedDictionaries and Maps: C++ comparedClasses and Objects: C++ comparedError Handling: C++ comparedReading Files: C++ compared