AppDividend
Latest Code Tutorials

References in C++ Example | C++ References Tutorial

0

References in C++ Example | C++ References Tutorial is today’s topic. References are somewhat similar to pointers, but not identical; there is a difference between  References and pointers. References declared a second name to the existing variable. And the symbol “&” is used to declare a variable reference. If we declare a variable reference, it can be accessed by either the name of the variable or reference name. It is effortless and easy to declare references as it is declared by a simple “&.”And can be accessed by a special ‘.’ operator.

References in C++

Till now, we’ve discussed two basic variable types:

  1. Normal variables, which hold values directly.
  2. Pointer variables, which hold the address of another value (or null) and can be dereferenced to retrieve the value at that address they point to.

References are actually the third basic type of variable that C++ supports. The reference is a type of C++ variable that acts as an alias to another object or value.

C++ supports three kinds of references:

  1. References to non-const values (typically just called “references”, or “non-const references”), which we’ll discuss in this lesson.
  2. References to const values (often called “const references”), which we’ll discuss in the next lesson.
  3. C++11 added r-value references, which we cover in detail in the chapter on move semantics.

#Difference between Pointer and Reference

A null pointer is present, but there is nothing like Null Reference. As Null pointer shows that they are not pointing to any variable.

If a reference refers to an object, they can’t be changed to refer to another object. But pointers any time can refer to any object. In a nutshell, references cannot be reseated.

Reference is initialized at the time of creation but pointers anywhere you want.

References will be accessed by ‘.’ operator (Dot operator) but pointer will be accessed by ‘->’ operator (Arrow). 

Because of all the reasons we cannot use References for implementing data structures in C++ For Linked List, Trees and many more.

#Similarities between Pointers and References

Use to change local variables of a function in any other function.

Otherwise, it can’t happen. So it’s a fair use of pointers and references to change the local variables; otherwise, it’s tough to use functions in programs.

Example: Suppose we have made a program to swap two numbers, but we have made a separate function for swapping and we give parameters to that function from our program

And when both the numbers get swap then if we have not used pointers or references then on returning we don’t get swapped result. 

It saves one copy of objects when arguments are passed in function.

*Now the question arises how we can create References.

#include<iostream>
using namespace std;
int main()
{
  int x = 10;
  int &ref = x; (Way of declaration)
  ref = 25;
  x = 35;
}

In C++ we pass reference as function parameter and references can also be returned in C++ as we do with any of the other datatypes.

#Powers of References

References in C++ can’t be used for implementing data structures but in Java can be used for implementing data structures. That’s why Java does not contain pointers in it and is a powerful programming language.

#Application

If the value of any variable get changed in the function than it gets changed in the whole program.

See the following program.

#include <iostream>
using namespace std;

void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

int main()
{
    int a = 20, b = 30;
    swap(a, b);
    cout << "After Swap Value of a is " << a << " and b is " << b << "\n";
}

See the following output.

 

References in C++ Example

Copy of large structures should be avoided as a new copy is created there is a wastage of  CPU time and memory. In loops for modifying all object. In loops so that no copy of object occurs.

#Avoiding copy of large structures

Let’s take a function that has to receive a large object. If we pass it without reference, a new copy of it is created which causes wastage of CPU time and memory. We can use references to avoid this.

See the following code.

#include<iostream>

using namespace std;

struct Employee { 
   string name; 
   string address; 
   int eNo;
};

void main(const Employee &e)
{ 
    cout << e.name << "  " << e.address << "  " << e.eNo;
}

Finally, References in C++ Example | C++ References Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.