/    /  CPP – Dynamic Initialization of variables

Dynamic Initialization of Variables

 

The process of initializing a variable at the moment it is declared at runtime is called dynamic initialization of the variable. Thus, during the dynamic initialization of a variable, a value is assigned to execution when it is declared.

 

Example:

main()
{
Int a;
 cout<<“Enter Value of a”;
cin>>a;
int cube = a * a * a;
}

In the above example, the variable cube is initialized at run time using expression a * a * an at the time of its declaration.

 

Reference Variables

A reference variable allows us to create an alternative name for the already defined variable. It is introduced in C++. Once you define a reference variable that references an already defined variable then you can use any of them alternatively in the program. Both refer to the same memory location. Thus, if you change the value of any of the variables it will affect both variables because one variable references another variable. 

 

The general syntax for creating a reference variable is as follows:

Data-type & Referace_Name = Variable_Name;

The reference variable must be initialized during the declaration.

 

Example:

int count;

count = 5;

Int & incre = count;

Here, we have already defined a variable named count.

 

Then we create a reference variable named incre that refers to the variable count. Since both variables refer to the same memory location if you change the value of variable count using the following statement:

count=count + 5;

Will modify the variable count value and increment to 10. The major use of the reference variable is in function. If we want to pass the reference of the variable at the time of function call so that the function works with the original variable we can use the reference variable.