Thursday, December 8, 2011

Reference variable : Demo

//reference.cpp

#include<iostream>

//Syntax of reference variable
//data_type & reference_name = variable_name;

using namespace std;
main()
{
 int x ;
 x = 10;
 int &y = x; // reference MUST always be initialized at declaration
 cout << "\nx = " << x << "   and y = " << y;
 y = 5;
 cout << "\nx = " << x << "   and y = " << y;
 x = 15;
 cout << "\nx = " << x << "   and y = " << y;
}

//Description:
//Lvalue : Location (address) of an expression
//Rvalue : VAlue of expression
//e.g. -
//  int a,b;
//a=4;  // Rvalue 4 is stored in Lvalue of a
// b = a;//Ralue of a is stored in Lvalue o fb



Think of REFERENCE variable as a constant pointer that is deREFERENCEd each time it is used!

No comments:

Post a Comment