In this topic, you will learn about, C++ Reference variable.
C++ supports one more type of variable called reference variable, in addition to the value variable and pointer variable C. Value variables are used to hold some numerical values; pointer variables are used to hold the address of (pointer to) some other value variable. The reference variable behaves similarly to both, a value variable and a pointer variable. In a program code, it is used similar to that of a value variable but has an action of a pointer variable.
Syntax:- Datatype & ReferenceVariable = ValueVariable;
Example:
1 2 3 |
int x=10; int &y=x; x/y |
Program to show the use of reference variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<iostream.h> #include<conio.h> void main() { int a=10; int &b=a; clrscr(); cout<<"a="<<a<<"\tb="<<b<<endl; a=20; cout<<"a="<<a<<"\tb="<<b<<endl; b=30; cout<<"a="<<a<<"\tb="<<b<<endl; cout<<"&a="<<&a<<"\t&b="<<&b<<endl; getch(); } |
Comment below if you have queries related to the above topic, C++ Reference variable.