Passing objects as function arguments in C++

Updated

We know that functions take arguments of valid data type.Objects are of type class which is user defined and can be used as function argument.
There are two ways in which objects can be passed as arguments to a function:
1.A copy of the entire object can be passed to the function.
2.Only the address of the object is transferred to the function.
The first method is called as pass-by-value.
Here,a copy of the object is passed to the function and any changes made to the object inside the function does not affect the object that was used in function call to call the function.
The second technique is called as pass-by-reference.
Here,the address of the object is passed to the function and not the entire copy of the object.
so when there is any changes made to the object inside the function will affect the object used to call the function.
Pass-by-reference is more effective when compared to pass-by-value because we send only the address of the object rather than the entire copy of the object.

Three memory diagrams: d1 as a copy of a, d1 holding the address of a, and d1 as another name for a.
//Write CPP code here
#include<iostream>
using namespace std;

class Distance
{
 int km;
 int m;
public:
 void read()
{
 cout<<endl<<"Enter the kilometer:";
 cin>>km;
 cout<<"Enter the meter:";
 cin>>m;
 km+=m/1000;
 m=m%1000;
}
void sum(Distance d1,Distance d2)
{
 m=d1.m+d2.m;
 km=d1.km+d2.km;
 km+=m/1000;
 m=m%1000;
}
void display()
{
 cout<<endl<<"Kilometers:"<<km<<endl<<"Meters:"<<m;
}
};

int main()
{
Distance a,b,c;
cout<<"A:\n";
a.read();
cout<<"B:\n";
b.read();
c.sum(a,b);
cout<<"C:\n";
c.display();
return 0;
}

Output : A:
Enter the kilometer:10
Enter the meter:1001
B:
Enter the kilometer:5
Enter the meter:10
C:
Kilometers:16
Meters:11
Explanation : Here, a and b are the objects passed as function argument.
Since entire copy of object is sent,it is pass-by value.
d1 is the copy of a.
d2 is the copy of b.
Inside the function, these parameters are used.Any changes made to d1 and d2 won’t effect a and b objects.

//Write CPP code here
#include<iostream>
using namespace std;
class Distance
{
 int km;
 int m;
public:
 void read()
{
 cout<<endl<<"Enter the kilometer:";
 cin>>km;
 cout<<"Enter the meter:";
 cin>>m;
 km+=m/1000;
 m=m%1000;
}
void sum(Distance *d1,Distance *d2)
{
 m=d1->m+d2->m;
 km=d1->km+d2->km;
 km+=m/1000;
 m=m%1000;
}
void display()
{
 cout<<endl<<"Kilometers:"<<km<<endl<<"Meters:"<<m;
}
};

int main()
{
Distance a,b,c;
cout<<"A:\n";
a.read();
cout<<"B:\n";
b.read();
c.sum(&a,&b);
cout<<"C:\n";
c.display();
return 0;
}

Output : A:
Enter the kilometer:10
Enter the meter:1001
B:
Enter the kilometer:5
Enter the meter:10
C:
Kilometers:16
Meters:11
Explanation : Here, the address of a and b are transferred hence the arguments in function header should be pointer.
d1 and d2 are pointers to a and b respectively.Hence we can manipulate a and b using d1 and d2 respectively.

//Write CPP code here
#include<iostream>
using namespace std;
class Distance
{
 int km;
 int m;
public:
 void read()
{
 cout<<endl<<"Enter the kilometer:";
 cin>>km;
 cout<<"Enter the meter:";
 cin>>m;
 km+=m/1000;
 m=m%1000;
}
void sum(Distance &d1,Distance &d2)
{
 m=d1.m+d2.m;
 km=d1.km+d2.km;
 km+=m/1000;
 m=m%1000;
}
void display()
{
 cout<<endl<<"Kilometers:"<<km<<endl<<"Meters:"<<m;
}
};

int main()
{
Distance a,b,c;
cout<<"A:\n";
a.read();
cout<<"B:\n";
b.read();
c.sum(a,b);
cout<<"C:\n";
c.display();
return 0;
}

Output : A:
Enter the kilometer:10
Enter the meter:1001
B:
Enter the kilometer:5
Enter the meter:10
C:
Kilometers:16
Meters:11
Explanation : Here,a and b are transferred.Since d1 and d2 are reference variable,any changes made to d1 and d2 will affect a and b respectively.

Comment
Mithlesh Upadhyay Published 12 Nov 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • Delete an element or a range from a set in C++ STL

    Delete an element from the Set by passing its value : Approach : In this method, an element is deleted by using the erase function and calling it…

    3 min read
  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage?️. Tools are scattered everywhere,…

    4 min read
  • What is Overriding in C++

    Write a program in C++ to depict the concept of overriding or Dynamic polymorphism. Overriding is a type of polymorphism. Polymorphism in OOP languages means to take more…

    2 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read