Intro to C++

Raw power and speed. The language behind game engines, operating systems, and everything that needs to be fast.

1

The Story of C++

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.

1972
Dennis Ritchie creates the C programming language at Bell Labs to build the Unix operating system. C gives programmers direct control over hardware while being portable across machines.
1979
Bjarne Stroustrup, also at Bell Labs, begins working on "C with Classes". He wants the power and speed of C, but with support for organizing code into objects and classes.
1985
C++ is officially released. The name is a programmer joke: the ++ operator in C means "increment by one." C++ is C, incremented. One step beyond.
1998
The first ISO standard for C++ is published (C++98), making it an international standard with a defined specification.
2011+
Modern C++ arrives (C++11, C++14, C++17, C++20) with smart pointers, lambdas, auto types, and other features that make the language safer and more expressive while keeping its speed.

C++ powers an enormous range of software today:

UE
Unreal Engine
PC
Windows OS
WEB
Chrome
FOX
Firefox
ART
Adobe Apps
IC
Embedded Systems
CAR
Self-Driving Cars
MRS
Mars Rover
Fun Fact

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.

2

Hello, World!

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:

1#include <iostream>
2
3using namespace std;
4
5int main() {
6    cout << "Hello, World!" << endl;
7    return 0;
8}

Let's break down every line:

#include <iostream>

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.

using namespace std;

The standard library lives inside a "namespace" called std. This line lets you write cout instead of std::cout. It is a convenience shortcut.

int main()

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 << "..." << endl;

cout sends text to the console. The << operator pushes data into the output stream. endl adds a newline and flushes the buffer.

return 0;

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.

{ } Curly Braces

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.

cout vs printf

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.

3

Variables and Types

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.

TypeWhat it holdsExample
intWhole numbers42, -7, 0
doubleDecimal numbers3.14, -0.5, 99.9
stringText"hello", "C++"
boolTrue or falsetrue, false
charSingle character'A', 'z', '9'
1int age = 14;
2double gpa = 3.85;
3string name = "Alice";
4bool enrolled = true;
5char grade = 'A';
6
7cout << name << " is " << age << " years old" << endl;
8// Output: Alice is 14 years old

Compare this with Python, where types are inferred automatically:

C++ Static Types

int x = 10;
string msg = "hi";
// x = "text"; ERROR!

You must declare the type. The compiler rejects mismatches.

Python Dynamic Types

x = 10
msg = "hi"
x = "text" # Fine!

No type declarations. Variables can change type freely.

Why Static Typing?

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.

4

Control Flow

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.

If / Else

1int score = 85;
2
3if (score >= 90) {
4    cout << "A" << endl;
5} else if (score >= 80) {
6    cout << "B" << endl;
7} else {
8    cout << "Keep trying" << endl;
9}
10// Output: B

The Classic For Loop

1for (int i = 0; i < 5; i++) {
2    cout << "i = " << i << endl;
3}
4// Output: i = 0, i = 1, i = 2, i = 3, i = 4

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.

While Loop

1int countdown = 3;
2
3while (countdown > 0) {
4    cout << countdown << "..." << endl;
5    countdown--;
6}
7cout << "Go!" << endl;
8// Output: 3... 2... 1... Go!

C++ Syntax

if (x > 10) {
    // braces required
}

Python Syntax

if x > 10:
    # indentation required

C++ uses parentheses around conditions and curly braces around blocks. Python uses colons and indentation instead. Both approaches work, just different conventions.

5

What Makes C++ Special

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.

Compiled to Machine Code

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.

Manual Memory Control

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.

Templates and Generics

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.

Object-Oriented

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.

Zero-Cost Abstractions

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.

40+ Years of Libraries

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.

Where C++ Engineers Work

Game Development

Unreal Engine, Unity internals, AAA game studios

Systems Programming

Operating systems, drivers, embedded firmware

Finance

High-frequency trading, risk engines, real-time analytics

Embedded / IoT

Microcontrollers, robots, medical devices, automotive

Browsers and Engines

Chrome (V8), Firefox (SpiderMonkey), WebKit

Graphics and VFX

Rendering engines, Pixar tools, Adobe Creative Suite

6

Try It Yourself

The terminal has a C++ compiler built in. Install it, write a program, compile and run it. Here is the full workflow:

1
Install the compiler
apt install gcc
2
Create a file
nano hello.cpp opens the editor. Write your C++ program inside.
3
Compile and run
gcc hello.cpp compiles your code and runs it immediately.

Here is a starter program you can copy into the editor:

#include <iostream>
using namespace std;

int main() {
    string name;
    cout << "What is your name? ";
    cin >> name;
    cout << "Hello, " << name << "! Welcome to C++." << endl;
    return 0;
}
The Compile Step

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.

C++ Coder!

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.

0
Sections Explored
0
Time Exploring

Compiled Language

C++ code is compiled directly to machine code before it runs. No interpreter, no virtual machine. This is why C++ programs are so fast.

Types Matter

Every variable in C++ must have a declared type: int, double, string, bool. The compiler catches type errors before your program ever runs.

Close to the Hardware

C++ gives you direct control over memory allocation and pointers. You decide when memory is created and destroyed. With great power comes great responsibility.

Foundation of Modern Software

Game engines, browsers, operating systems, databases, embedded systems. The software that runs the world is written in C and C++.

Ready to Create?

Put your new knowledge into practice!

Suggest a Correction