Introduction to C++

C++ started life as "C with Classes" — Bjarne Stroustrup's attempt to add object-oriented features to C without giving up C's speed or its direct access to memory. Decades later, the "Classes" part has grown into references, templates, exceptions, and a large standard library, but the core deal hasn't changed: you still get to control exactly how your program uses memory and CPU time, with a lot more language support for organizing large codebases than C ever offered.

What C++ adds on top of C

Almost every valid C program can be compiled as C++ with little or no change, because C++ is (mostly) a superset of C. What it adds is everything that makes organizing a large program less painful:

  • Classes let you bundle data and the functions that operate on it into one unit, instead of passing structs around to a pile of separate functions.
  • References give you an alternative to pointers for passing values around without copying them, without the sharp edges of pointer syntax.
  • Function and operator overloading let you write add(2, 3) and add(2.5, 3.1) as two different functions with the same name, or make + mean something sensible for your own types.
  • The Standard Template Library (STL) hands you dynamic arrays, maps, sorting, and searching — data structures and algorithms that a C programmer would otherwise hand-roll for every project.
  • Stronger type checking catches more mistakes at compile time than C's more permissive rules allow.

None of this makes C++ "better" than C in some abstract sense — C is still the right tool when you need the absolute smallest, simplest possible compiled output, which is why operating system kernels and embedded firmware often stay in C. C++ earns its keep on larger programs, where the extra structure pays for itself.

Compiling a C++ program

C++ is a compiled language: you write source code in a .cpp file, and a compiler translates it into a binary your machine can run directly. The most common compiler is g++, part of the GNU Compiler Collection. Given a file named hello.cpp, you compile and run it like this:

$ terminal
g++ hello.cpp -o hello
./hello

The -o hello flag names the output binary hello; without it, g++ would name the binary a.out by default. Every time you change the source file, you need to recompile before your changes take effect — unlike a scripting language, there's no way to just re-run the source file directly.

A first program

Here's the smallest useful C++ program: it prints a line of text and exits.

</> hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}
Terminal output
Hello, world!

Breaking that down line by line:

  • #include <iostream> pulls in the input/output library that defines std::cout. It's a preprocessor directive, not a C++ statement — notice it has no semicolon.
  • int main() is the function every C++ program starts running from. Execution begins at the first line inside its braces.
  • std::cout ("character output") is the standard output stream — normally your terminal. The << operator sends whatever's on its right into the stream on its left, and you can chain several in a row.
  • std::endl writes a newline and flushes the output stream. You'll also see "\n" used instead — it's slightly cheaper because it skips the explicit flush, and either is fine while you're learning.
  • return 0; tells the operating system the program finished successfully. A nonzero return value is a program's way of reporting that something went wrong.

Why the std:: prefix

Everything from the standard library — cout, string, vector, and so on — lives inside a namespace called std, and std::cout means "the cout that belongs to std." Namespaces exist so that two different libraries can each define something called, say, Timer, without colliding — you'd write std::Timer and mylib::Timer to tell them apart. You'll sometimes see older code start with using namespace std; to drop the prefix everywhere, which saves typing but reintroduces exactly the naming collisions namespaces were meant to prevent. This course spells out std:: every time, which is also what you'll find in most modern C++ style guides.

Note: a C++ source file's extension is usually .cpp, though you'll also see .cc or .cxx in older or cross-platform codebases — all three tell the compiler to expect C++ rather than plain C.