C++ Reference Variables

C++ Reference variable

C++ introduces a new kind of variables known as the reference variables provide a alias(alternative name) for a previously defined variables. for example, if we make the variable sum a reference to the variable total then sum and total can be used interchangeable to represent that variable or represent the sum . A Example of reference variable is created as follows
Example

float total=150;
float sum=total;

in the above example total is a float type variable that has already been declared. sum is the alternative name declared to representthe variable total. both the variable refer to the same data object in memory. nor the statements

cout << total;
and
cout << sum;
both print the value 150. The statement total=total+15; will change the value of both total and the sum to 165. likewise, the assigment sum=0; will change the value of both the variable to zero. A reference variable must be initialized at the time of declaration.This establishes the correspondence between the reference and data object which it names. it is important to note that the initilization of a reference variable is completely different from assignment to it.

c++ assigns additional meaning to the symbol &. here & is not an address operator. The notation float & means reference to float. other example are

int a[10];
int &x=a[10]; // The x is alias for a[10]
char &y=’\n’; // initialize reference to a literal

the variable x is an alternative to the array element a[10]. The variable y is initialized to the newline constant. This created a reference to the otherwise unknown location where the newline constant \n is stored the variable x is an alternative to the array element a[10]. The variable y is initialized to the newline constant. This created a reference to the otherwise unknown location where the newline constant \n is stored the following references are also allowed

1. int x;
int *p=&x;
int &m=*p;
2. int &n=50;

the first set of declaration causes m to refer to x which is pointed to by the pointer p and the statement in (2) creates an int object with value 50 and name n.

A major application of reference variable is in passing arguments to functions, consider the folowing

void f(int &x) // uses reference
{
x=x+10; // x is incremented so also m
}
int main()
{
int m=10;
f(m); // f() function call
……..
……..
}

where the function call f(m) is executed, the following initialization occurs int &x=m;
thus x becomes an alias of m after execution the statement
f(m);
let us understand

in the above example , function calls are known as call by reference. this implementation see in the pic . since the variable x and m are alias. when the function increments x,m is also incremented. The value of m becomes 20 after the function is executed. in traditional c, we accomplish this operation using pointers and dereferencing techniques

reference variable in c++

The call by reference mechanism is useful in object-oriented programming because it permits the manipulation of object by reference, and eliminates the copying of object parameters back and forth. it is also important to note . that references can be created not only for built in data types but also for user defined data types such as structures and classes. references work wonderfully well with these user-defined data types

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise