>4.1 Write a program to swap the private data of two classes using friend function.
>
/*Write a program to swap the private data of two classes using friend function.
Programmed by: KiranShrestha
Roll No.: 10121
Programmed on: 31st July, 2011*/
#include <iostream.h>
#include <conio.h>
class b;
class a
{
int x;
public:
void assign(int t)
{
x =t;
}
void display()
{
cout<<“value of x is: “<<x<<endl;
}
friend void swap(a&,b&);
};
class b
{
int y;
public:
void assign(int w)
{
y=w;
}
void display()
{
cout<<“value of y is: “<<y<<endl;
}
friend void swap(a&,b&);
};
void swap(a &c,b&d)
{
int temp;
temp =c.x;
c.x = d.y;
d.y = temp;
}
int main()
{
a n;
b m;
inta,b;
cout<<“enter x =”;
cin>>a ;
cout<<“Enter y =”;
cin>>b;
n.assign(a);
m.assign(b);
swap(n,m);
n.display();
m.display();
getch();
}
Output Sample
Enter x= 4
Enter y= 6
Value of x is: 6
Comments (3)
thank you