Introduction

Introduction in C++: Python is an automatic. You press the pedal, the car decides the gear, and you get where you are going without thinking about the gearbox.

The Analogy: Driving a Manual Car

Python is an automatic. You press the pedal, the car decides the gear, and you get where you are going without thinking about the gearbox. It is comfortable, and it is a little slower.

C++ is a manual with the bonnet open. You choose the gear, you choose when memory is allocated and freed, and you can even reach in and adjust the engine while it runs. Nothing is hidden from you and nothing is done on your behalf. That is why C++ powers game engines, operating systems, browsers, databases, and high-frequency trading systems: when you need the last ten percent of performance, you need control over the details.

The trade-off is real. The compiler will let you drive off a cliff. This course is about learning to steer.


Syntax or Core Structure

C++ was created by Bjarne Stroustrup in 1985 as "C with Classes". Every C++ program starts from the same skeleton:

#include <iostream>

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

Line by line:

  • #include <iostream> pulls in the input/output stream library. The angle brackets mean "look in the standard library".
  • int main() is the entry point. Every program has exactly one, and it returns an int to the operating system.
  • std::cout is the standard character output stream; << is the insertion operator, feeding data into it.
  • std::endl writes a newline and flushes the buffer. "\n" writes a newline without flushing, and is usually faster.
  • return 0; tells the OS the program succeeded. Non-zero means failure.
// You can chain insertions -- they evaluate left to right
int score = 42;
std::cout << "Score: " << score << " points\n";

Technical Depth: Compiled, Not Interpreted

This is the single biggest difference from Python or JavaScript, and it shapes everything else.

g++ -std=c++20 -Wall -Wextra -o hello hello.cpp   # compile
./hello                                            # run

Your .cpp file is translated into machine code before it runs, in four stages:

  1. Preprocessing — #include and #define are expanded textually.
  2. Compilation — each .cpp becomes an object file of machine code.
  3. Assembly — the object file is turned into raw bytes.
  4. Linking — object files and libraries are stitched into one executable.

Two consequences follow. First, type errors and typos are caught before the program ever runs — there is no such thing as a C++ program that crashes on line 400 because of a misspelled variable. Second, the running program has no interpreter, no virtual machine, and no garbage collector between it and the CPU.

// The strictness that buys you that speed
int x = 5;
x = "hello";        // Error: cannot convert 'const char*' to 'int'
std::cout << "Hi"   // Error: expected ';'

The three rules that trip up newcomers

  • Every statement ends in a semicolon. Not a newline — a semicolon.
  • Blocks use braces, not indentation. Indentation is for humans only.
  • Every variable has a declared type, fixed for its lifetime. int x = 5;, never bare x = 5.
// std:: means "from the standard namespace"
#include <iostream>
using namespace std;      // brings everything into scope -- convenient, risky

int main() {
    cout << "No std:: needed now\n";
    return 0;
}

You will see using namespace std; in tutorials everywhere. It is fine in a 20-line exercise and a genuinely bad habit in real code: it dumps thousands of names into your scope, so your count variable can silently collide with std::count. Prefer typing std::.


Real World Example: A Complete First Program

#include <iostream>
#include <string>

int main() {
    std::string name;
    int year = 0;

    std::cout << "What is your name? ";
    std::getline(std::cin, name);

    std::cout << "What year were you born? ";
    std::cin >> year;

    const int currentYear = 2026;
    int age = currentYear - year;

    std::cout << "Hello, " << name << "! You are about " << age << " years old.\n";

    if (age >= 18) {
        std::cout << "You can vote.\n";
    } else {
        std::cout << "You have " << (18 - age) << " years to wait.\n";
    }

    return 0;
}

Everything in this program is deliberate. #include <string> is needed because std::string lives in its own header — unlike Python, nothing is available by default. std::getline reads a whole line including spaces, while std::cin >> year reads a single token. int year = 0; is initialised because an uninitialised int in C++ contains whatever garbage was in that memory, and reading it is undefined behaviour.


C++ Versus What You Know

C++PythonJavaScript
Executioncompiled to machine codeinterpretedJIT compiled
Typingstatic, checked at compile timedynamicdynamic
Memorymanual or RAIIgarbage collectedgarbage collected
Speedfastest of the threeslowestin between
Entry pointint main()top of filetop of file
Errors foundmostly at compile timeat runtimeat runtime

Common Mistakes

  • Forgetting the semicolon — the error often points at the next line, which is confusing until you learn to look up.
  • Missing #include for something you use: std::string needs <string>, std::vector needs <vector>.
  • Writing cout without std:: and without using namespace std;.
  • Getting the stream arrows backwards: cout << sends data out, cin >> reads data in.
  • Leaving variables uninitialised and then reading them — undefined behaviour, not a zero.
  • Compiling without warnings on: always use -Wall -Wextra. The warnings are the compiler telling you where the bugs are.
  • Expecting main to need arguments — int main() is complete and valid on its own.

Best Practices

  • Always compile with -std=c++20 -Wall -Wextra and fix every warning.
  • Prefer std:: prefixes over using namespace std; from your very first program.
  • Initialise every variable at the point of declaration.
  • Use "\n" rather than std::endl unless you specifically need the flush.
  • Keep main short: read input, call functions, return.
  • Compile early and often — a small error found now is easier than five errors found later.

Practice Exercises

  1. Hello, Compiler: Write and compile a program that prints your name, then deliberately delete a semicolon and read the exact error message the compiler gives you.
  2. The Interview: Extend the program above to also ask for a favourite language and print a sentence combining all three answers.
  3. The Flags: Compile the same file with and without -Wall -Wextra after adding an unused variable, and compare the output.