Page 57 - DCAP404 _Object Oriented Programming
P. 57

Object-oriented Programming




                    Notes          2.6.4 Reference  Variables

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

                                   data_type  &  reference_name  =  variable_name

                                          Example:

                                   float  total  =  100;
                                   float  &sum  =  total;
                                   total is a float type variable that has already been declared, sum is the alternative name declared
                                   to represent the variable total. Both the variables refer to the same data object in the memory.
                                   Now, the statements
                                   cout  <<  total;
                                   and

                                   cout  <<  sum;
                                   both print the value 100. The statement
                                   total  =  total  +  10;
                                   will change the value of both total and sum to 110. Likewise, the assignment
                                   sum  =  0;
                                   will change the value of both the variables to zero.
                                   A  reference variable  must  be  initialized  at  the  time  of  declaration.  This  establishes  the
                                   correspondence between the reference and the data object that it names. Note that the initialization
                                   of a reference variable is completely different from assignment.
                                   Note that C++ assigns additional meaning to the symbol &. Here, & is not an address operator.
                                   The notation float & means reference to float. Some more examples are presented below  to
                                   illustrate this point:
                                   int n[10];
                                   int &x = n[10];      //x is alias for n[10]
                                   char &a = ‘\n’;      // initialize reference to a literal
                                   The variable x is an alternative to the array element n[10]. The variable a is initialized to the new
                                   line constant. This creates a reference to the otherwise unknown location where the new line
                                   constant \n is stored.
                                   The following references are also allowed:
                                   1.  int x;
                                       int *p=&x;
                                       int &m = *p;

                                   2.  int &n = 50;





          50                                LOVELY PROFESSIONAL UNIVERSITY
   52   53   54   55   56   57   58   59   60   61   62