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.

//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.