• A reference variable is an alias, that is, another name for an already existing
variable.
• Once a reference is initialized with a variable, either the variable name or the
reference name may be used to refer to the variable.
• A reference variable is a "reference" to an existing variable, and it is created
with the & operator.
• Syntax :
• Datatype & reference _name = variable _name;
• Where reference name is a new name given to the variable.
• How to create a reference?
• Reference can be created by simply using an ampersand (&) operator.
• When we create a variable, then it occupies some memory location. We can create a reference of
the variable; therefore, we can access the original variable by using either name of the variable or
reference.
• For example,int a=10;
• Now, we create the reference variable of the above variable.
• int &ref=a;int &ref=a;
• The above statement means that 'ref' is a reference variable of 'a', i.e., we can use the 'ref' variable
in place of 'a' variable.
• ex:
• main() x Location name
•{ 100 Location value
• Int x=100;
1002 Location address
• Int y=x;
• cout<<x; Y Location name
• cout<<y; 100 Location value
• x++;
1004 Location address
• cout<<x;
• cout<<y;
•}
x/y Location name
• Ex : 100 Location value
• Int x =100; 1002 Location address
• Int &y =x;
• ex:
• main() x/y Location name
•{ 100 Location value
• Int x=100; 1002 Location address
• Int &y=x;
• cout<<x; // o/p 100 x/Y Location name
• cout<<y; // o/p 100
110 Location value
• x= x+10;
1002 Location address
• cout<<x; //o/p 110
• cout<<y; // o/p 110
#include <iostream>
using namespace std;
int main () {
// declare simple variables
int i;
double d;
// declare reference variables
int &r = i;
double& s = d;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
Table 1
Value of i : 5
Value of i re ference : 5
Value of d : 11.7
Value of d re ference : 11.7
#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}
Output:
x = 20
ref = 30
C++ Scope resolution operator
•#
• # include<iostream.h> • # include<iostream.h>
• Int i=10; • Int i=10;
• Void main() • Void main()
•{ •{
• Int j=20; • Int i=20;
• Cout <<i<<endl; • Cout <<i<<endl;
• cout<<j; •}
•}
• o/p
• o/p • 20
• 10
• 20
• main()
• {
• Int i=5;
• {
Inner block Outer block
• i=10;
• cout<<i; // 10
• }
• }
• A variable I.e declared inside a function is called as “local
variable”.
• A variable declared outside function is called as “global
variable”
• Whenever there is conflict between local and global variable
then local variable gets priority .
• Global variable is inaccessible when local variable of same
name is available within the function
• Cpp allows the user the flexibility of accessing both the variable
.it achieve this with the help of new operator called as “scope
resolution operator “ .
#include<iostream.h> M
Int m=10;
10
main()
{
1002
Int m=20;
Int k=m; M
{
20
cout<<“we are in inner block”;
cout<<“k=“<<k; // 20
cout<<“m=“<<m; // 20
1004
K
cout<<“:: m=“<<:: m; //
}
20
cout<<“we are in outer block”;
cout<<“m=“<<m;
1006
cout<<“::m”<<::m;