C++ is a popular programming language used for high performance applications such as web servers and operating systems, as well as many modern game engines. In this tutorial we will cover the basics of the older C++03 standard released in 2003, as well as mixing in the modern components introduced in the much newer C++11. Very minimal knowledge of programming will be assumed, so if you’ve already had exposure to languages similar to C++ or with C++ itself this tutorial may seem a little slow! Nonetheless, it’s a bit more fast paced than the previous version and will cover more material.

To get a quick taster of the language and to help explain a few things before we get started, here’s a short program that sums a sum numbers and then prints the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This line is necessary to be able to output information to the screen
#include <iostream>

// The program starts here and carries on line by line
int main()
{
    // Create two integers a and b containing 10 and 5
    int a = 10;
    int b = 5;

    /* Add them together and store the result in another
       integer called sum */
    int sum = a + b;

    // Output the sum to the screen
    std::cout << sum << std::endl;

    // End the program and send a value of 0 (success) back
    // to the operating system
    return 0;
}

This program is saved to a file, say tutorial.cpp and must be compiled by a C++ compiler before it can be run. Compilation is essentially the process of converting the code written by the programmer(s) into an executable file that the computer can run.

The exact process of compiling the program depends on your compiler, but once you’ve compiled the above code (something I encourage you to do) you should open up a command prompt/terminal window and run it. Note that double-clicking the compiled executable won’t do anything, you have to run the program in a command prompt/terminal such as cmd.exe in Windows.

> $ example-program
15

This is what we would expect!

Now let’s briefly examine a few features of the code. The first thing to note is the lines starting with //, which are ignored by the compiler and are used to add comments to the code which explain what the program is doing without affecting what it does.

Every line of a comment must begin with //, but you can save some typing by enclosing the entire comment in a comment block by using /* and */. Anything between those two delimiters will be commented out and ignored.

The first non-comment line is an include directive, which tells the compiler that we want to use code from other places that we may or may not have written ourselves. In this case we want to use code from a part of the standard library, iostream, which will allow us to output text to the screen.

The standard library is a collection of C++ code files (headers) that you can use in your programs and that are guaranteed to exist for everyone else writing code in C++. The file used here (it probably has the .hpp file extension and is installed somewhere else on your computer) includes code for manipulating input/output streams, which allows us to output information back to the user.

Finally there’s the line int main(), which is one of several variations on a line which must be present exactly once in every C++ program. Without it the compiler doesn’t know where the program should start (programs can be made of multiple files so it isn’t as simple as just starting from the first line and moving to the next, though that’s what happens most of the time). For now, everything that you want the program to do should go in between the two {}.

Remember to include the int main() when you run any of the code in this tutorial, because I’ll often leave it out!