×

C++ References

C++ References

An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use either the important variables or the relation name to refer to the variable.

References vs. Pointers

References are always confused with indicators, but there are three significant differences between references and indicators:-

  • You are not allowed to have NULL references. You must always be in a position to imagine that comparison is linked to a legitimate storage part.
  • Upon initialization of a reference to an entity, it could not be changed to refer to another object. At any instance of time points may be pointing to another thing.
  • When it is generated, a reference has to be initialized. Points should also be initialized.

Creating References in C++

Find a word for the variable as a label attached to the location in memory of the variable. Then you can think of a reference as a second label attached to that place of memory. Thus, either the original variable name or the reference can be used to access the contents of the variable. For example, suppose we have the example below:-

int k = 20;

We can declare variables of reference as follows for k.

int& s = k;

Read the & as a response in those declarations. Hence, read the first declaration as "s is an integer description configured to k and read the second statement as" s would be an optimized double definition to t." Take the following example with references on int and double:-

#include <iostream>
using namespace std;
int main () {
   // declare simple variables
   int    k;
   double t;
   // declare reference variables
   int&    s = k;
   double& a = t;
   k = 8;
   cout << "Value of k : " << k << endl;
   cout << "Value of k reference : " << s  << endl;
   t = 15.2;
   cout << "Value of t : " << t << endl;
   cout << "Value of t reference : " << a  << endl;
   return 0;
}

Output:

C++ References

Example no. 2:-

#include<iostream>

using namespace std;

int main()

{

  int y = 20;

  // ref is a reference to y.

  int& refr = y;

  // Value of y is now changed to 20

  refr = 40;

  cout << "y = " << y << endl ;

  // Value of y is now changed to 30

  y = 25;

  cout << "refr = " << refr << endl ;

  return 0;

}

Output:

C++ References

Applications

Modify the passed parameters in a function: When a function obtains a variable reference, it can change the data values. For example, inputs are switched using references in the following program.

#include<iostream>
using namespace std;
void swap (int& firstname, int& secondname)
{
    int temp = firstname;
    firstname = secondname;
    secondname = temp;
}
int main()
{
    int j = 4, k = 8;
    swap( j, k );
    cout << j << " " << k;
    return 0;
}

Output:

C++ References

Avoiding a copy of large structures: Picture a device that has a broad object to obtain. If we pass that without regard, a new copy of it will be created, which will cause CPU memory and time wastage. To stop this, we should use the references.

References are less powerful than pointers.

  • The reference to some other attribute may not be made later whenever a comparison is formed; it could not be repartitioned. Often this is achieved using pointers.
  • References are not NULL. Points are also rendered NULL to show they do not point to something important.
  • Upon declaration, a reference must be configured. There is no such restriction with pointers because of the limitations above, references in C++ cannot be used to enforce data structures such as Linked List, Tree. In Java, examples have no limitations above and can be used to implement all data structures. References being stronger in Java is the primary reason that Java does not need pointers.

Related Topics

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Dynamic _Cast in C++

C++ is one of the most powerful programming languages. We can write object-oriented and structured programming with the help of C++. In this article, we will learn about the dynamic...

3 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.