What C++ gives you
You already know some Python, so you know what variables, loops, and functions are. C++ gives you the same ideas with two big differences: it is compiled, and it is statically typed. In exchange it runs 10 to 100 times faster than Python, which is why game engines, operating systems, trading systems, and browsers are written in it. It is also a favorite language for coding interviews, because it is fast and its standard library maps cleanly onto data structures and algorithms.
In Python you run a program by handing the source file to an interpreter, which reads it line by line while the program runs. C++ works in two separate steps:
- A compiler, a program such as
g++orclang++, reads your whole.cppfile and translates it into machine code, the raw instructions your CPU executes. The result is a standalone executable file. - That executable runs on its own. The compiler is no longer involved.
This is the single most important mental-model shift from Python: errors can now happen at compile time, where the compiler refuses to produce an executable, or at run time, where the executable is built but misbehaves.
Anatomy of the smallest program
#include <iostream> int main() { std::cout << "Hello, C++!\n"; return 0; }
Line by line:
#include <iostream>pulls in the input/output stream library so you can print. It is roughly Python'simport, done before compilation.int main() { ... }is the entry point. Every C++ program starts by runningmain. Python starts at the top of the file, C++ always starts here.std::coutis the standard output stream (character out).std::means it lives in the standard library's namespace.<<sends what is on its right into the stream on its left.\nis a newline character.return 0;tells the operating system the program finished successfully.- Every statement ends with a semicolon, and blocks are wrapped in
{ }instead of using indentation.
The smallest complete program
Every piece of the program below is required, which is what makes it worth reading closely. There is no shorter C++ program that prints a line.
#include <iostream> int main() { std::cout << "Hello, C++!\n"; return 0; }
Output
Hello, C++!
The #include has to come before any use of std::cout, because the compiler reads the file top to bottom and needs to have been told what cout is before it meets it. The return 0; at the end reports success to the operating system, and while main is the one function allowed to omit it, writing it out makes the intent explicit.
When a typo is discovered
A missing semicolon, or any other syntax mistake, is caught at compile time, and the compiler refuses to build the executable at all.
The compiler reads the entire file before anything runs, so a syntax error means no executable is produced and therefore nothing ever executes. That differs sharply from Python, where a file can start running, print several lines, and only crash once the interpreter reaches the bad line.
Compile-time strictness feels unhelpful at first, especially when the error messages are long. It pays for itself by catching whole categories of mistakes before a program is ever handed to anyone.
| Error kind | Found by | Symptom |
|---|---|---|
syntax, such as a missing ; | the compiler | no executable is produced |
type mismatch, such as int x = "hi"; | the compiler | no executable is produced |
| bad logic, such as an off-by-one loop | running the program | wrong output |
| bad memory use, such as a stray pointer | running the program | a crash or corrupted data |
Printing two lines
Adding a second line of output means adding a second statement, since each std::cout statement writes exactly what it is given and nothing more.
#include <iostream> int main() { std::cout << "Hello, C++!\n"; std::cout << "Goodbye, Python.\n"; return 0; }
Output
Hello, C++! Goodbye, Python.
The \n at the end of each string is what puts the two outputs on separate lines. Without it, cout would run them together as Hello, C++!Goodbye, Python. on one line, because C++ never adds a newline for you the way Python's print does.
Both statements end in a semicolon, which is the rule for every statement in the language. Indentation inside main is purely for human readers here, since the braces are what actually group the code.