C++
Introduction to C++
Every single thing in the C domain is still true here — C++ is a superset, not a different language. This chapter is specifically about what's added: a first look at a class, how compiling C++ differs slightly from C, and a map of the bigger features the rest of this domain covers one at a time.
- What it is
- A language built as a strict superset of C, adding object-oriented programming, generic programming (templates), and a large standard library (the STL) on top.
- Mental model
- C gives you data and the functions that operate on it as two separate things you connect by hand; C++'s biggest addition is letting you bundle them together into one unit — a class.
- Key numbers
- Files conventionally use
.cpp/.cc(not.c) and.hpp/.hfor headers; compiled withg++/clang++rather thangcc. - Reach for it when
- A C codebase's manual "struct + functions that take it as the first argument" pattern is common enough that bundling them directly would simplify the code, or when templates/STL containers would remove real, repeated boilerplate.
What's actually different from C
C already lets you group data with struct
and write functions that operate on it — the pattern is just
manual: you pass the struct as the function's first argument every single time, and nothing stops an
unrelated function from reaching in and modifying fields it shouldn't. C++'s core addition,
classes, bundles data and the functions that operate on it into one unit, with real,
compiler-enforced control over what's accessible from outside.
A first class, line by line
class Counter {
public:
Counter() { count = 0; } // constructor — runs automatically on creation
void increment() { count++; } // a method — a function that belongs to the class
int getValue() { return count; }
private:
int count; // data only this class's own methods can touch
};
int main() {
Counter c; // no "struct" keyword needed, and the constructor runs automatically
c.increment();
c.increment();
std::cout << c.getValue() << std::endl; // 2
return 0;
}
class Counter { ... };- Declares a new type,
Counter— likestruct, but with methods and access control built in directly. public:/private:- Everything after
public:is accessible from outside the class; everything afterprivate:is only accessible from the class's own methods. This is real, compiler-enforced encapsulation, covered fully in Classes & Objects. Counter()- A constructor — a special method with the same name as the class, run automatically the instant an object is created. No separate "init" call needed.
std::cout- C++'s standard output stream — the idiomatic replacement for C's
printf, from the<iostream>header.
Compiling C++
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}
g++ hello.cpp -o hello
./hello
# Hello, C++!
The same four-stage pipeline from C's Introduction
(preprocess, compile, assemble, link) applies unchanged — g++ is simply the C++ front end
for the same underlying toolchain, understanding the additional syntax and linking against the C++
standard library automatically.
A map of what's ahead
- Classes & Objects — the encapsulation idea above, in full depth.
- Constructors, Destructors & RAII — automatic setup and teardown, and the resource-management idiom built on it.
- Inheritance & Polymorphism — building one class on top of another, and calling the right version of a method automatically.
- Operator Overloading & References — making custom types work with
+,==, and similar, plus an alias-like alternative to pointers. - Templates — writing one function or class that works across many types, without repeating code per type.
- STL Containers & Algorithms — ready-made, templated data structures (
vector,map) replacing much of what C makes you hand-roll. - Memory Management —
new/deleteand smart pointers, built on top of everything Embedded C's Dynamic Memory already covers aboutmalloc/free. - Exception Handling — a structured alternative to C's manual error-code checking.
- C++ in Embedded Systems — what a bare-metal C++ codebase typically avoids from this whole list, and why.
Real-world applications
- Game engines — Unreal Engine and most AAA game engines are written in C++ for the combination of low-level control and object-oriented structure.
- High-performance desktop/server software — Chrome, most database engines' query planners, and large portions of major operating systems' userspace tooling.
- Robotics and embedded systems with real complexity — ROS (Robot Operating System) is written substantially in C++, valued for combining C-level performance with classes for organizing genuinely large codebases.
Gotchas
- C++ is almost a strict superset of C, not a perfect one — a handful of edge cases differ (most notably, C++ requires more explicit casting than C does in a few places). In practice this rarely matters for typical code.
- A
.cfile and a.cppfile with otherwise-identical code can behave subtly differently once compiled by their respective compilers — always use the file extension matching the language you're actually writing. - C++ is a genuinely large language — nobody uses every feature, and production C++ codebases very commonly adopt a deliberately restricted subset (see this domain's eventual C++ in Embedded Systems chapter for exactly what and why on constrained targets).