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.

Quick reference
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/.h for headers; compiled with g++/clang++ rather than gcc.
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 — like struct, but with methods and access control built in directly.
public: / private:
Everything after public: is accessible from outside the class; everything after private: 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

Real-world applications

Gotchas

Field notes