Skip to content

1. Hello, World!

The first code snippet one typically writes in any programming language is the "Hello, World!" program which prints "Hello, World!" on the terminal when executed. Such a program is very simple in most programming languages, and is often used to illustrate the basic syntax of a programming language. It is often the first program written by people learning to code. It can also be used as a sanity test to make sure that a computer language is correctly installed, and that the operator understands how to use it.

Starting the First Project

Open the terminal of your operating system. You should be in your home directory. Test this by using pwd like in the following case.

$ pwd
/home/lyrahgames

Create a folder where to store your course data and change the current directory to it. Check again with pwd.

$ mkdir -p projects/cpp-course
$ cd projects/cpp-course
$ pwd
/home/lyrahgames/projects/cpp-course

Create a folder for the first project in the course and change into it.

$ mkdir 01-hello
$ cd 01-hello
$ pwd
/home/lyrahgames/projects/cpp-course/01-hello

Create a file by the following command and open your text editor in the course directory.

touch main.cpp
subl ..

Click on the created file main.cpp in the folder 01-hello and type the following code.

#include <iostream>
using namespace std;
int main(){
  cout << "Hello, World!\n";
}

Make sure you have saved the changes. You can even do this in the terminal by directly outputting the content of the file using the cat command.

$ pwd
/home/lyrahgames/projects/cpp-course/01-hello
$ ls
main.cpp
$ cat main.cpp
#include <iostream>
using namespace std;
int main(){
  cout << "Hello, World!\n";
}

We compile the program in the same folder and run the created executable.

$ g++ main.cpp
$ ./a.out
Hello, World!

The Minimal C++ Program

The minimal C++ program, which does nothing, but is valid, is given by the following source code. Please note, that C++ is a key-sensitive language and therefore makes a difference between capital and non-capital letters.

int main() {}

This code defines the so-called main function of the program. main is one of the keywords of the C++ programming language and cannot be used in another place. The main function defines the unique entry point for a program. A computer then runs the executable code starting at this entry point.

The open and closed round Brackets () mean that the main function does not take any input arguments. Later, we will see that there is another possibility.

The curly braces {} express grouping in C++ and indicate the start and end of the function body. The function body defines the implementation of a function and therefore contains the code that the function should execute. In the case of the main function the program starts by executing the first command enclosed by curly braces.

In front of the function the respective return type is defined. In the case of the main function it has to be the type int. int is an abbreviation for integer and describes some sort of integral number. int is again a keyword of the language and can only be used in the context of a type.

The value returned by main is the program's return value to the system. In most cases a non-zero value indicates failure (and a zero value success). If there is no return value this means the program has succeeded. We do not have to explicitly state the program succession but we could do with the following code snippet.

int main() {
    return 0;
}

Here return is another keyword of the C++ programming language which defines the output value of a function AND exits from this function. Therefore if the return directive is executed then every following command will be ignored.

Producing Output on the Terminal

Typical programs should do something! Therefore we start with one of the most basic steps of printing 'Hello, World!' on the terminal. The following code snippet demonstrates this example.

#include <iostream>
int main() {
  std::cout << "Hello, World!\n";
}

The command #include <iostream> instructs the C++ preprocessor to include all declarations (not the implementations) of the I/O routines of the C++ standard library found in the file 'iostream'. Only with these declarations the command std::cout << "Hello, World!\n"; makes sense.

cout is the standard object for printing information on the console. To use the formatted output, we write the streaming operator << before the argument "Hello, World!\n". The argument "Hello, World!\n" is a string literal which means that it is a sequence of characters surrounded by double quotes. In C++ the backslash \ in a string literal is an escape character to make it possible to write special characters, like the newline character \n or the tab character \t. At the end of every command there has to be a semicolon ;.

std is a so-called namespace which shall be explained in more detail later. You only have to remember that every standard facility is put in the namespace std (an abbreviation for 'standard'). To use any function or class in this namespace we have three main possibilities. - preceding the command by std:: to make the namespace explicit like it was already shown - defining a using namespace std; directive somewhere in the file to make anything available - defining a using std::cout; directive to make only cout available

Both examples will be shown for convenience.

#include <iostream>
using namespace std;
int main() {
    cout << "Hello, World!\n";
}

#include <iostream>
int main() {
    using std::cout;
    cout << "Hello, World!\n";
}

Why are we doing this?

Every program consists of an input, work that has to be done, and output. Without input and output, work that is done is useless. In my experience, handling input and output will take up more than 80 % of your working time. Only with proper input and output concepts, a program becomes usable and testable. As a consequence, we first have to understand the basic input and output facilities in C++.

Give Me Your Name and Age

Open a terminal or use an already opened terminal. Go into the course folder and start a new project.

$ cd ~/projects/cpp-course
$ mkdir 02-input
$ ls
01-hello    02-input

Create a new main.cpp file.

$ cd 02-input
$ touch main.cpp
$ ls
main.cpp

Open Sublime Text and open the file main.cpp in the folder 02-input. Add the following lines.

#include <iostream>
#include <string>

using namespace std;

int main() {
  cout << "Please give me your name: ";

  string name;
  string surname;
  cin >> name;
  cin >> surname;

  cout << "Please give me your age: ";
  int age;
  cin >> age;

  cout << "Your name is '" << name << " " << surname << "'.\n"
       << "Your current age is " << age
       << " years and at your next birthday you will become " << age + 1
       << " years.\n";
}

We compile the program in the same folder and run the created executable.

$ g++ main.cpp
$ ./a.out
Please give me your name: lyrah games
Please give me your age: 123
Your name is 'lyrah games'.
Your current age is 123 years and at your next birthday you will become 124 years.

Simple Idea of Types and Variables

In C++, objects and variables are synonyms. The same holds for types, classes, and structures. Every variable belongs to a unique type. Types are only known to the compiler and vanish after the code was compiled and transformed into an executable. Variables store actual data in memory represented by binary numbers. But the types tell us how to interpret those binary numbers. For example, think of a mathematical expression like the following.

\[ x\in\mathbb{R} \]

In this case, \(x\) is a variable of the set \(\mathbb{R}\). We interpret \(\mathbb{R}\) as the type of \(x\). So a type is more or less a set of all objects with certain constraints or requirements.


Last update: October 22, 2020