Skip to content

Prerequisites

This course starts at objects, not at variables. This page states precisely what it assumes, and refreshes the one topic that most students arrive without.


What you must already be able to do

You are ready to start Module 1 if all of the following are true. Check yourself honestly — it costs one hour now to avoid ten hours of confusion later.

  • [ ] Compile and run a program in some language, and read a basic error message.
  • [ ] Declare and use variables of fundamental types, and know the difference between an integer, a floating-point value, a character and a boolean.
  • [ ] Use operators — arithmetic, comparison, logical — and understand precedence well enough to use parentheses when unsure.
  • [ ] Write control flow: if/else, switch, while, do/while, for.
  • [ ] Write and call functions, with parameters and return values.
  • [ ] Use arrays or lists of values, and access their elements by index.
  • [ ] Accept input and produce output — from the console is enough.

If any item is not yet true, that is useful information, not a failure. A good move is to write a few small programs — a temperature converter, a program that prints the multiplication table, one that finds the largest value in an array — until the list is true. Those are exactly the exercises of a first programming module, and this course will still be here afterwards.

What you do not need

You do not need any previous experience with classes, objects, pointers, references, templates, or any other language's object model. Everything from Module 2 onwards is taught from the beginning here.

You also do not need Linux, macOS, or any particular editor. See Set Up Your Toolchain.


Refresher: values, references, and pointers

This is the one topic students most often arrive without, and Module 1 uses it immediately. Read it now; you will meet it properly again inside the course.

A variable in C++ is three separate ideas at once, and keeping them apart is most of the battle:

Idea Meaning
The object The region of memory holding the value itself
The name How your code refers to that object
The address Where that object lives in memory

Passing by value copies

#include <iostream>

void addOneByValue(int n) {
    n = n + 1;              // changes this function's own copy
}

int main() {
    int count = 5;
    addOneByValue(count);
    std::cout << count << '\n';   // prints 5 — the caller's variable never changed
}

A reference is another name for an existing object

A reference is written with & in the parameter list. It does not copy, and there is nothing to "dereference" — using the reference is using the original object.

#include <iostream>

void addOneByReference(int& n) {
    n = n + 1;              // changes the caller's variable
}

int main() {
    int count = 5;
    addOneByReference(count);
    std::cout << count << '\n';   // prints 6
}

A reference must be initialised when it is created and it cannot be made to refer to a different object later.

A pointer stores an address

A pointer is a variable whose value is an address. You take an address with & and follow ("dereference") it with *:

#include <iostream>

int main() {
    int count = 5;
    int* p = &count;             // p holds the address of count

    std::cout << count << '\n';  // 5     — the value
    std::cout << p     << '\n';  // some address, printed in hexadecimal
    std::cout << *p    << '\n';  // 5     — the value at that address

    *p = 9;                      // writes through the pointer
    std::cout << count << '\n';  // 9     — count changed
}

A pointer may also be nullptr, meaning it points at nothing. A reference can never be null.

The rule of thumb the rest of the course builds on

  • Use values by default.
  • Use const references when passing something large that you will not modify.
  • Use non-const references when a function must modify the caller's object.
  • Use pointers only when you genuinely need "may refer to nothing that exists", or later, when you need to manage an object's lifetime explicitly — a topic Modules 10 and 11 are entirely about.

Dereferencing a pointer that does not point at a live object — a dangling pointer — is undefined behaviour: the C++ standard gives no guarantee about what happens. The program may crash, may print nonsense, and may appear to work. "This ran fine on my machine" is not evidence that the code is correct. The course returns to this repeatedly, because it is the single most common source of serious bugs in C++.


A 10-minute self-check

Try these before starting Module 1. If you can do all three, you are ready.

  1. Write a program that reads integers from the user until a negative number is entered, then prints how many were read and their sum.
  2. Write a function double average(double a, double b) and call it from main, printing the result to two decimal places.
  3. Write a function that takes an array of integers and its length, and returns the largest value. Then write a second version that takes a std::vector<int> by const reference.

Question 3's second half uses material from Module 3 onwards — but if you can work out how to write it, you are well prepared.


Comment on the code snippets on this page

The snippets here are short and illustrative. You can compile any of them by saving the code to a file ending in .cpp and running:

g++ -std=c++17 -Wall -Wextra your_file.cpp -o your_file

Every one of them compiles cleanly under that command, which is the same command the whole course uses. See Your First Program.


Next: Set Up Your Toolchain