Todays topic in our C++ tutorial is References .
We have seen function calling with argument(s) in C++ programs. The call may be using value or reference. When arguments are passed by value, the called function create new variables of the same type as the argument and copies the argument’s value into it. The function does not have direct access to the variables in the calling program, it deals only with the copies. Passing arguments by value is useful when the function does not need to modify the original value.
Passing arguments by reference uses a different technique in C++ programs. It passes the address of the location. In the called function pointer variables are used to hold the address of the location. The data type of pointer and the variable must be same.
A reference variable is like a pointer but with some difference. A reference provides an alias name for a variable.
Here is a C++ program to demonstrate use of reference variable.
#include< iostream.h >
#include< conio.h >
void main()
{
clrscr();
int i;
cout<< endl<< "Enter value:";
cin>>i;
int &j=i;
cout<< endl<< "Value through variable="<< i;
cout<< endl<< "Value through reference variable="<< j;
i+=10;
cout<< endl<< "Value through variable(after i+=10)="<< i;
cout<< endl<< "Value through reference variable(after a+=10)="<< j;
j-=5;
cout<< endl<< "Value through variable(after j-=5)="<< i;
cout<< endl<< "Value through reference variable(after j-=5)="<< j;
getch();
}
In the above C++ program j is the reference variable. The declaration syntax of a reference variable is using the & operator before the variable name.
1. Any reference variable while declared must be initialized .
2. Once a reference variable is defined to refer to a particular variable, it can not refer to any other variable. Value of any other variable can be assigned on any one of them, both the variable and the reference variable will refer to the same value.
#include< iostream.h >
#include< conio.h >
void main()
{
clrscr();
int i=10;
int x=20;
int &j=i;
cout<< endl<< i<<","<< j;
j=x;
cout<< endl<< i<<","<< j; /* both will display 20 */
getch();
}
In the above C++ program j is called reference of i.
1. A variable and it’s reference are so tightly inter-locked that any modification on either of them will have the same effect on the other.
2. A variable may have a number of references. Changing the value of any one of them effects the same change in all of them.
#include< iostream.h >
#include< conio.h >
void main()
{
clrscr();
int i;
int &j=i;
int &x=i;
cout<< endl<< "Enter value:";
cin>>i;
cout<< endl<< "Value through variable="<< i;
cout<< endl<< "Value through 1st reference variable="<< j;
cout<< endl<< "Value through 2nd reference variable="<< x;
i+=10;
cout<< endl<< "Value through variable(after i+=10)="<< i;
cout<< endl<<"Value through 1st reference variable(after a+=10)="<< j;
cout<< endl<< "Value through 2nd reference variable="<< x;
j-=5;
cout<< endl<< "Value through variable(after j-=5)="<< i;
cout<< endl<< "Value through 1st reference variable(after j-=5)="<< j;
cout<< endl<< "Value through 2nd reference variable="<< x;
getch();
}
No comments:
Post a Comment