}
In C++ variables can be declared anywhere in the program which will allow to do dynamic initialization. Remember for dynamic initialization the variables must be declared prior to dynamic initialization of new variable.
Reference Variable:
Syntax to define a reference variable is
datatype &reference_variable_name = variable_name;
int y;
int &x = y;
Now the value of x and y will be the same and also if any one of the value is changed the
change will be reflected in the other variable. y =10; Now x is also 10.
x = 20; Now y is also 20.
y = x + 10; Now x and y is 30.
Reference through pointer variable:
int x;
int *ptr = &x;
int &y = *ptr; This is equivalent to int &y=x;
In function it is known as call by reference void f_ref(int &x)
{x = x +10;
}
int main ( )
{int m =10; f_ref(m); return 0;
}