VARIABLES IN C++
Declaration of variables.
Dynamic initialization of variables.
In c++, a variable can be initialized at run time using expressions at the place of declaration. This is refereed to as dynamic initialization.
Ex.- int m = 10;
Here variable m is declared and initialized at the same time.
Reference variables.
ii) Example:- If we make the variable sum a reference to the variable total, them sum & total can be used interchangeably to represent that variable.
iii) Reference variable is created as:-
data type & reference name = variable name.
Ex:- int sum = 200; int &total = sum;
Here total is the alternative name declared to represent the variable sum. Both variable refer to the same data object in memory.
iv) A reference variable must be initialized at the time of declaration.
v) C++ assigns additional meaning to the symbol '&'.Here & is not an address operation. The notation int & means reference to int. A major application of reference variable is in passing arguments to functions.
Ex. void fun (int &x) // uses reference
{
x = x + 10; // x increment, so x also incremented.
}
int main( )
{
int n = 10; fun(n); // function call
}
When the function call fun(n) is executed, it will assign x to n
i.e. int &x = n;
Therefore x and n are aliases & when function increments x. n is also incremented. This type of function call is called call by reference.
ALSO SEARCH:-
"variables in C++ define and declare variable"
"variables in C++ define and declare array"
"variables in C++ define and declare a function"
"variables in C++ definition"
"define variable in C++"
"reference variable in C++"
"data types in C++ with examples"
"dynamic initialization of variables in C++"
"variable initialization in C++"