Intro to C++
Raw power and speed. The language behind game engines, operating systems, and everything that needs to be fast.
Raw power and speed. The language behind game engines, operating systems, and everything that needs to be fast.
C++ is one of the most influential programming languages ever created. It started as an extension of C, the language that built Unix, and grew into the backbone of modern high-performance software.
C++ powers an enormous range of software today:
The Mars Rover's flight software is written in C++. When NASA needed a language they could trust with a $2.7 billion mission on another planet, they chose C++. There is no "restart" button on Mars.
Every programming journey starts with "Hello, World!" In C++, that means learning about includes, the main function, and output streams. Here is the simplest complete C++ program:
Let's break down every line:
This tells the compiler to include the Input/Output Stream library. It gives you cout (console output) and cin (console input). Without it, the program cannot print anything.
The standard library lives inside a "namespace" called std. This line lets you write cout instead of std::cout. It is a convenience shortcut.
Every C++ program starts running from main(). The int means it returns a number. The operating system uses this number to know if the program succeeded or failed.
cout sends text to the console. The << operator pushes data into the output stream. endl adds a newline and flushes the buffer.
Returning 0 from main tells the operating system: "Everything went fine." A non-zero return means something went wrong. This convention is used by scripts and build systems to detect errors.
Braces define a block of code. Everything between { and } belongs to the function. Every opening brace needs a closing brace. Every statement ends with a semicolon.
C uses printf("Hello\n") for output. C++ introduced cout with the << operator, which is type-safe and extensible. You can print strings, numbers, and custom objects the same way. Most modern C++ code uses iostream, not printf.
C++ is a statically typed language. That means every variable must have its type declared when you create it. The compiler checks types at compile time, catching bugs before the program runs.
| Type | What it holds | Example |
|---|---|---|
| int | Whole numbers | 42, -7, 0 |
| double | Decimal numbers | 3.14, -0.5, 99.9 |
| string | Text | "hello", "C++" |
| bool | True or false | true, false |
| char | Single character | 'A', 'z', '9' |
Compare this with Python, where types are inferred automatically:
You must declare the type. The compiler rejects mismatches.
No type declarations. Variables can change type freely.
Static types catch errors at compile time, before anyone runs the program. In a million-line codebase (like a game engine or operating system), catching a type mismatch early can save days of debugging. It also lets the compiler optimize code more aggressively because it knows exactly what type every variable is.
Control flow determines which code runs and how many times. C++ uses braces to define blocks, not indentation like Python. The three essentials: if/else for decisions, for loops for counting, and while loops for repeating until a condition changes.
The for loop has three parts separated by semicolons: init (int i = 0), condition (i < 5), and update (i++). It runs as long as the condition is true.
C++ uses parentheses around conditions and curly braces around blocks. Python uses colons and indentation instead. Both approaches work, just different conventions.
Many languages exist. So why does C++ remain essential after 40 years? Because no other mainstream language gives you this combination of speed, control, and abstraction.
C++ compiles directly to the native machine code your CPU understands. No interpreter, no virtual machine, no garbage collector pausing your program. This is why C++ programs are among the fastest software in existence.
You decide when memory is allocated and freed. In languages like Python or JavaScript, a garbage collector handles this automatically but unpredictably. C++ lets you be precise, which matters when every microsecond counts.
Write a sorting function once that works with any data type: integers, strings, custom objects. Templates generate specialized code at compile time, so you get flexibility without sacrificing speed.
Classes, inheritance, polymorphism, encapsulation. C++ lets you model complex systems as interacting objects. A game engine might have classes for Player, Enemy, Weapon, and World, each with their own data and behavior.
C++ is designed so that high-level features (like classes and templates) compile down to code that is just as fast as if you wrote the low-level version by hand. You do not pay a performance penalty for writing clean code.
Decades of libraries for graphics (OpenGL, Vulkan), physics (Bullet, PhysX), networking, audio, cryptography, and more. If it needs to be fast, someone has written a C++ library for it.
Unreal Engine, Unity internals, AAA game studios
Operating systems, drivers, embedded firmware
High-frequency trading, risk engines, real-time analytics
Microcontrollers, robots, medical devices, automotive
Chrome (V8), Firefox (SpiderMonkey), WebKit
Rendering engines, Pixar tools, Adobe Creative Suite
The terminal has a C++ compiler built in. Install it, write a program, compile and run it. Here is the full workflow:
apt install gccnano hello.cpp opens the editor. Write your C++ program inside.gcc hello.cpp compiles your code and runs it immediately.Here is a starter program you can copy into the editor:
Unlike Python or JavaScript, C++ code must be compiled before it runs. The compiler translates your human-readable code into machine instructions that the CPU executes directly. This extra step is why C++ programs start up instantly and run faster than interpreted languages. The compiler also checks your code for errors, catching mistakes before the program ever executes.
You now know the fundamentals of one of the most powerful programming languages ever created. C++ gives you direct control over hardware and memory, which is why it powers the software that powers everything else.
C++ code is compiled directly to machine code before it runs. No interpreter, no virtual machine. This is why C++ programs are so fast.
Every variable in C++ must have a declared type: int, double, string, bool. The compiler catches type errors before your program ever runs.
C++ gives you direct control over memory allocation and pointers. You decide when memory is created and destroyed. With great power comes great responsibility.
Game engines, browsers, operating systems, databases, embedded systems. The software that runs the world is written in C and C++.
Put your new knowledge into practice!