C++ Tutorial
A practical C++ tutorial for working programmers who want to get productive in modern C++. Covers tooling, memory management, OOP, modern C++ (C++11 through C++23), the STL, build systems, and best practices.
Chapters
About this tutorial
A practical tour of modern C++ for programmers coming from another language. Get productive without pretending the language is small.
Who This Is For
- Programmers fluent in another language (Python, Go, Rust, JavaScript) who need to write or read C++
- Engineers moving into systems, games, finance, or embedded work
- Anyone who has touched C++ before and wants the modern (C++11 to C++23) version
Contents
Fundamentals
- Introduction: What C++ is for, where it sits in the language landscape, and how this tutorial is shaped
- Tooling Setup: Compilers, the compilation model, debuggers, sanitizers, formatters
- Basic Syntax: Types, variables, control flow, functions, lambdas, strings, arrays
Core Concepts
- Memory Management: Stack and heap, pointers, references, RAII, smart pointers, move semantics
- Object-Oriented Programming: Classes, the rule of five, operator overloading, inheritance, polymorphism
Modern C++
- Modern C++: C++11 through C++23 features, ranges, concepts, coroutines, std::expected
- The STL: Containers, iterators, algorithms, when to reach for which container
Ecosystem
- Build Systems: CMake, package managers (vcpkg, Conan), warnings, sanitizers
- Best Practices: Memory safety, function design, common pitfalls, performance tips
Mastery
- Practical Projects: A task manager, a generic Vector, a thread pool, a parser, a memory pool
- Ecosystem Guide: Standards, terminology, compilers, libraries, community, careers
How to Use This Tutorial
- Read sequentially for the full path. Chapter 3 (memory) is the hinge; do not skip it.
- Type out the examples. C++ rewards muscle memory.
- Build something after each pair of chapters. Suggestions live in chapter exercises.
Quick Reference
Essential Commands
# Compile a single file with warnings on
g++ -std=c++20 -Wall -Wextra -Wpedantic file.cpp -o program
# Debug build with sanitizers
g++ -std=c++20 -Wall -g -fsanitize=address,undefined file.cpp -o program
# Release build
g++ -std=c++20 -O2 -DNDEBUG file.cpp -o program
# CMake configure and build
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
# Run tests
ctest --test-dir build --output-on-failure
Hello World
#include <iostream>
int main() {
std::cout << "Hello, C++!\n";
return 0;
}
Common Patterns
// RAII over manual cleanup
auto buffer = std::make_unique<int[]>(1024);
// Range-based for with const reference
for (const auto& item : items) {
process(item);
}
// std::optional for "might not exist"
std::optional<int> findIndex(std::span<const int> v, int target);
// Structured bindings
for (const auto& [key, value] : map) {
std::cout << key << ": " << value << "\n";
}
// Move into container
container.emplace_back(std::move(value));
Learning Path Suggestions
Working Programmer (4 to 6 weeks)
- Chapters 0 to 2: setup and syntax (a weekend)
- Chapter 3: memory model. Sit with it.
- Chapters 4 to 6: OOP, modern features, STL
- Build the task manager from chapter 9
- Chapters 7 to 8: build systems and idioms
- Pick one more project from chapter 9
Reviewer or Maintainer (1 to 2 weeks)
- Chapter 3 (memory) and Chapter 8 (best practices). These two cover most code review failure modes.
- Skim Chapter 5 for which features show up in modern code.
- Chapter 10 for terminology and the compiler landscape.
Why C++?
- Performance with control. You decide where allocations happen.
- Zero-cost abstractions. High-level code compiles to tight machine code.
- Footprint. Operating systems, browsers, databases, game engines, trading systems.
- Longevity. The language has evolved every three years since C++11. Code from 2015 still compiles; code from 2024 looks completely different.
Additional Resources
- cppreference.com - the reference you will keep open
- C++ Core Guidelines - Stroustrup and Sutter
- Compiler Explorer - see your assembly
- CppCon on YouTube - conference archive
- C++ Weekly - short, regular tips from Jason Turner
C++ Version
This tutorial targets C++20 with notes on C++23 features. Examples compile with GCC 12+, Clang 15+, or MSVC 2022. C++17 fallbacks are mentioned where it matters.
A note on opinions: this tutorial picks a side on most contested questions (smart pointers over raw new, {} over () initialization, const references for read parameters, RAII over try/finally). The reasoning is in the prose. If you disagree, write the alternative and see how it ages.