Passing object by reference in C++

Today, we will learn about passing objects to a function by reference method in C++. In C++ programming, many times we need to reflect the changes in actual parameters. So, here we need to pass an object by reference. If you want to learn how to do it, then you are in the right place. Here we will see sample programs. Here, in this tutorial, we will differentiate between pass by value and by reference. And in the end, you will realize the importance of pass by reference in C++ programming.

Reference variable in C++

A reference variable is an alias of another previously defined variable. Once you define a reference variable for an already existing variable, you can use both names. A variable name refers to a memory block in which data is stored. Defining a reference variable gives another name for the same memory block.

void main()
{
   int a=5;
   int &b=a;
   cout<<"\na = "<<a;
   cout<<"\nb = "<<b;
}

In this code snippet, both the variable i.e. a and b will produce the same value. In this, there is only one integer variable having two different names namely a and b.

Pass by reference in C++

In pass by reference method in C++, the function creates a reference of the actual parameters. The changes made to the formal parameters are also reflected in the actual parameter values. In the call by reference method, the function prototype will be like –

return_type function_name(data_type &arg1,data_type &arg2,data_type &argn);

Thus in the pass by reference, an alias name is given to the actual parameters. The call by reference method is useful if you need to reflect on the changes done by a function. You can use a return statement if the function makes changes in exactly one parameter. But if multiple parameters are affected then call by reference will be useful.

C++ program for passing by reference

Now, we will see a program in C++ to illustrate passing objects by reference to a function. So, to illustrate this we will take a C++ class named Tourist.

#include<iostream>
using namespace std;
class Tourist
{
  int id;
  char name[30];
  float amount;

  public:
  void get_input(void);
  friend void modify(Tourist&,float);
  void display(void);
};
void Tourist::get_input()
{
  cout<<"ENTER TOURIST ID : ";
  cin>>id;
  cout<<"ENTER NAME : ";
  cin>>name;
  cout<<"ENTER AMOUNT PAID : ";
  cin>>amount;
}
void Tourist::display()
{
  cout<<"\nTOURIST ID : "<<id;
  cout<<"\nTOURIST NAME : "<<name;
  cout<<"\nAMOUNT PAID : "<<amount;
}
void modify(Tourist &t,float new_amt)
{
  t.amount=new_amt;
  cout<<"\tNEW AMOUNT FOR TOURIST "<<t.id<<" IS : "<<t.amount;
}
int main()
{
  float amt;
  Tourist t1;
  t1.get_input();
  cout<<"\n\n--BEFORE MODIFICATION--";
  t1.display();
  cout<<"\n\n\tENTER THE NEW AMOUNT : ";
  cin>>amt;
  modify(t1,amt);
  cout<<"\n\n\n--AFTER MODIFICATION--";
  t1.display();
}

In the above program, we have created a class Tourist having variables id, name and amount paid by a tourist. Using the function get_input() we take the input about tourist information from the user. The display() function displays the details of a tourist. Other than these functions we have made modify() function as a friend of class Tourist.

A friend function cannot access variables of a class object directly. So, we need to pass the class object as a parameter to the function. This passing can be of two types –

  • Pass by value
  • Pass by reference

Here, in this program, we have used pass by the reference method.

The output of the C++ program

The output of the above program is –

[email protected]:~/cpp$ g++ pass.cpp
[email protected]:~/cpp$ ./a.out
ENTER TOURIST ID : 11
ENTER NAME : siddharth
ENTER AMOUNT PAID : 1900


--BEFORE MODIFICATION--
TOURIST ID : 11
TOURIST NAME : siddharth
AMOUNT PAID : 1900

  ENTER THE NEW AMOUNT : 2100
  NEW AMOUNT FOR TOURIST 11 IS : 2100


--AFTER MODIFICATION--
TOURIST ID : 11
TOURIST NAME : siddharth
AMOUNT PAID : 2100
[email protected]:~/cpp$

So, in the output, the amount before modification is 1900. Then we call the modify() function using call by the reference method. Because of this, the modification also reflects in the actual parameter. In the main() function, the amount paid after calling modify() function is 2100. So, the value of the object is changed by the modify() function due to pass by reference.

Importance of pass by reference method in C++

In pass by value method, we create a new object whose scope is limited to the function only. The changes are done with this temporary object. But the actual object remains the same. The modify() function prototype using call by value method will be –

void modify(Tourist t, float new_amt);

Here, whatever the changes are made it will be with local object ‘t’.

There is a great difference in output using call by value and call by reference –

Output in the call by value method – 

[email protected]:~/cpp$ g++ pass1.cpp
[email protected]:~/cpp$ ./a.out
ENTER TOURIST ID : 11
ENTER NAME : siddharth
ENTER AMOUNT PAID : 1900


--BEFORE MODIFICATION--
TOURIST ID : 11
TOURIST NAME : siddharth
AMOUNT PAID : 1900

  ENTER THE NEW AMOUNT : 2100
  NEW AMOUNT FOR TOURIST 11 IS : 2100


--AFTER MODIFICATION--
TOURIST ID : 11
TOURIST NAME : siddharth
AMOUNT PAID : 1900
[email protected]:~/cpp$

You can see that the amount after modification does not change. It is still 1900 and no change occurs.

Output in the call by reference method –

[email protected]:~/cpp$ g++ pass.cpp
[email protected]:~/cpp$ ./a.out
ENTER TOURIST ID : 11
ENTER NAME : siddharth
ENTER AMOUNT PAID : 1900


--BEFORE MODIFICATION--
TOURIST ID : 11
TOURIST NAME : siddharth
AMOUNT PAID : 1900

  ENTER THE NEW AMOUNT : 2100
  NEW AMOUNT FOR TOURIST 11 IS : 2100


--AFTER MODIFICATION--
TOURIST ID : 11
TOURIST NAME : siddharth
AMOUNT PAID : 2100
[email protected]:~/cpp$

So, you can see the difference. In the call by reference method, the value of the amount changes. After modification, it is showing 2100. So the changes made in modify() function also reflects in the actual object.

Thanks a lot for reading this tutorial. I hope it helped you to clear the concept.

Also read: Pass NULL value as function parameter in C++

Leave a Reply

Your email address will not be published. Required fields are marked *