C++: reference

A fast compiled language: types, input and output, conditions and loops.

Program structure

Minimal program

The program starts from main. Every statement ends with a semicolon.

#include <iostream>
using namespace std;

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

Compilation

C++ is compiled into an executable file and then run.

g++ main.cpp -o main
./main

Variables and types

Basic types

The type is given at declaration. Integer division drops the fraction: 7 / 2 gives 3.

int age = 16;
double price = 24.5;
char letter = 'A';
bool ok = true;
string name = "Alex";    // required #include <string>
const double PI = 3.14;

Conversion

stoi and to_string are handy conversions between strings and numbers.

int n = (int) 3.9;          // 3
double d = 7 / 2.0;         // 3.5
int x = stoi("12");
string s = to_string(42);

Input and output

cin and cout

cin >> reads a word or a number, getline(cin, line) reads a whole line.

string name;
int age;
cout << "Name and age: ";
cin >> name >> age;
cout << name << ", " << age + 5 << endl;

Conditions

if and switch

One = assigns, two == compare. In switch do not forget break.

if (age < 12) {
    cout << "child" << endl;
} else if (age < 18) {
    cout << "teenager" << endl;
} else {
    cout << "adult" << endl;
}

switch (n) {
    case 1: cout << "one"; break;
    default: cout << "other";
}

Loops

for, while, do-while

do-while runs at least once.

for (int i = 0; i < 3; i++) {
    cout << i << " ";
}
int n = 0;
while (n < 3) { n++; }
do { n--; } while (n > 0);

Range-based loop

The form for (type x : collection) goes through the elements one by one.

int a[] = {1, 2, 3};
for (int x : a) {
    cout << x << " ";
}

Arrays and vectors

Array

Indexing starts from zero. Going out of the array bounds is a dangerous error.

int a[5] = {1, 2, 3};     // the rest are zeros
a[3] = 10;
cout << a[0] + a[3];

vector

vector is an array of variable size.

#include <vector>
#include <algorithm>

vector<int> v = {3, 1, 2};
v.push_back(5);
sort(v.begin(), v.end());
cout << v.size() << " " << v[0];

Functions

Declaration and call

A function must be declared before it is called.

int add(int a, int b) {
    return a + b;
}

void hello(string name) {
    cout << "Hello, " << name << endl;
}

Passing by reference

& passes the variable itself, not a copy.

void inc(int &x) {
    x++;
}

int n = 5;
inc(n);          // n == 6

Strings

string

Strings can be compared with == and joined with +.

string s = "C++";
cout << s.length() << s[0] << s.substr(1) << endl;
s += " 17";
cout << (s == "C++ 17") << endl;

Classes

Class and constructor

It is better to make fields private and give access through methods in the public section.

class Dog {
public:
    Dog(string n) : name(n) {}
    string speak() { return name + ": woof!"; }
private:
    string name;
};

Dog rex("Rex");
cout << rex.speak();

Pointers

Address and value

& takes the address, * gets the value at the address.

int x = 10;
int *p = &x;       // p stores an address x
*p = 20;           // x is now 20
cout << x << " " << *p;

Learn C++ with practice

Want to learn step by step? Your account has 110 lessons organised by chapter (80 core and 30 advanced, you can switch the level at any time) and tasks with code checking.