Code for Program that provides an example of passing objects to function using call by reference method in C++ Programming
#include<iostream.h>
class data2;
class data1
{
int a;
public:
void get(int no)
{
a=no;
}
friend void swap(data1 &, data2 &);
void show()
{
cout<<"a= "<<a<<endl;
}
};
class data2
{
int b;
public:
void get(int no)
{
b=no;
}
friend void swap(data1 &,data2 &);
void show()
{
cout<<"b= "<<b<<endl;
}
};
void swap(data1 &a1,data2 &a2)
{
int temp;
temp=a1.a;
a1.a=a2.b;
a2.b=temp;
}
main()
{
data1 a;
data2 b;
a.get(10);
b.get(20);
cout<<"\n\n Before swapping \n\n";
a.show();
b.show();
swap(a,b);
cout<<"\n\n After swapping\n\n";
a.show();
b.show();
}