How to Initialization of Variables in C++?
Initialization
of variables during the run time is called dynamic initialization of variables.
In C++, a variable can be initialized at run time using expressions at the
place of declaration. For example, the following are valid initialization
statements.
int i;
cin>>i;
float avg=i/10;
The variable avg is initialized only during run
time, because the value of i is
available during run time only. Thus
both the declaration and initialization of a variable can be done
simultaneously at the place where the variable is used for the first time. The
following two statements in the example
float average;
average = sum/i; //declare
where it is necessary
can be combined
into a single statement:
float average = sum / i; // initialize dynamically at run time
dynamic
initialization is extensively used in object-oriented programming.
No comments:
Post a Comment