C++ from scratch
From the first main() to move semantics, virtual dispatch, threads and the build toolchain — ending with a snake game you can play right here.
C++ gives you two things at once: abstractions as high-level as Python’s, and control over every byte as tight as C’s. Most of the difficulty — and most of the bugs — live where the two meet: who owns a piece of memory, when a value is copied and when it is merely moved, and what the compiler is actually allowed to assume.
This course follows the repository’s forty small programs in order, grouped into lessons, then turns to the tools around the language — the compiler, make, CMake and the debuggers — and finishes with the SFML snake game, ported to run live in the browser with its original textures.
Every program was compiled and run with g++ 13 while writing these notes. Two of them do not do what their comments say, and one does not compile; the lessons point these out, because each one teaches something real.
Prerequisites
- Comfort with any programming language: variables, loops, functions.
- A terminal. The C course helps for the memory lessons but is not required.
Running the code
Most programs are a single file. A few use C++20 features, so pass the standard explicitly:
cd cpp_from_scratch/basics
g++ -std=c++20 -Wall 12references.cpp -o references
./references
The snake game needs SFML 2.x and builds with its Makefile:
sudo apt install libsfml-dev
cd cpp_from_scratch/snake_game
make && ./bin/main
Lessons
- 1 First programsmain, printing, variables and auto, conditions, std::array and the three kinds of loop.
- 2 Functions, overloading and templatesOne name, many types — first by writing each version by hand, then by letting the compiler write them.
- 3 Iterators, algorithms and the standard libraryPositions instead of indices, half-open ranges, sorting, spans, random numbers and reading files.
- 4 std::vector and how it growsSize versus capacity, the doubling strategy, and why push_back is fast on average but occasionally slow.
- 5 References and pointersA copy is a new object, a reference is a new name, and a pointer is a new object that holds an address.
- 6 The heap and smart pointersnew and delete by hand, then unique_ptr and shared_ptr, which make ownership part of the type.
- 7 Classes, constructors and destructorsBundling data with behaviour, controlling how objects are created, copied and destroyed, and letting the compiler write comparisons.
- 8 Move semantics and emplace_backStealing a temporary's resources instead of copying them — and the one keyword that decides whether std::vector will let you.
- 9 Inheritance and polymorphismReusing a base class, then calling the right override through a base reference — what virtual really costs, and the diamond problem.
- 10 Class templates, lambdas, constexpr and conceptsGeneric containers, functions as objects, work done at compile time, and templates that state their requirements.
- 11 Threads, mutexes and atomicsRunning work in parallel, and the data race that silently loses updates when two threads share a counter.
- 12 The compiler, linker, make and CMakeWhat g++ actually does to a source file, how headers and libraries fit together, and what optimisation levels can and cannot do.
- 13 Finding memory bugsOff-by-one reads, leaks, double frees, use-after-free and heap overruns — and which tool catches which.
- 14 Project: a snake game with SFMLA complete game built from the course's pieces — a state stack of unique_ptrs, virtual functions, a fixed-timestep loop and a snake that recycles its tail. Playable below.