Type casting in cpp

 Type casting.{short}


Reference variable:-
Reference variable produces an aliens ( alternative name) feel a precously defined variable.
Syntax:-
data type &reference_variable_nm=variable_nm;
Ex:-int sum=100;
int &total=sum;
⇒ handsome is the inter type variable that has already been declared and total is the alternative name declared to the present variable sum.
cout<<sum;
cout<<total;
⇒ Both statement prints the value 100
Sum=sum+20;
Will change the value of both total and sum.
⇒A reference variable must be initialised at the time of declaration.
⇒Hence, both the variable sum and the total reference to the same letter object in the memory.
⇒Hence, "&" percent is not an address operator.
Program on reference variable.
#include<iostream.h>
int main()
{
int sum=200;
int & total=sum;
cout<<"sum="<<sum<<"\ntotal="<<total;
sum=sum+30;
cout<<"\nsum="<<sum<<"\ntotal="<<total;
    total=total+30;
    cout<<"sum="<<sum<<"\ntotal="<<total;
    return 0;
}
Out put:-
sum=200
total=200
sum=230
total=230
sum=260
total=260
[Program finished]

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat