How are references declared

In our example below, a reference y is created and is
made to reference x. A reference can only be set to
reference a variable when it is declared. In other words, y can now not be used to
reference any other variable but x.

int x = 5;
int &y = x;
int z = 20;

We now increase x by 10.
When printing out the values of x
and y, we find that they both equal 15. This is because
they both reference the same data in memory. If you change the value of one, you
change the value of the other.

x += 10;
cout << "x : " << x << endl;
cout << "y : " << y << endl;

Remember me saying that a reference can never reference another variable? In the example
below, z is assigned to y.
This does not make y reference z.
It only assigns the value of z to y.
Because y references x,
x now also has the value of z.

y = z;
cout << "x : " << x << endl;
cout << "y : " << y << endl;

To try and make things more clear, you can output the memory address of each
variable. You will notice that x and
y reference the same memory address where
z has a different memory address.
This strongly shows how references work.