Tuesday, April 3, 2012

Create Friend function which shoud be able to swap the value of data members of two subjects of same class.


#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
friend void swap(abc &,abc &);
void read()
{
cout<<"Enter values of a and b";
cin>>a>>b;
}
void show()
{
cout<<a<<endl<<b<<endl;
}
};
void swap(abc &x1,abc &x2)
{
int x,y;
x=x1.a;
y=x1.b;
x1.a=x2.a;
x1.b=x2.b;
x2.a=x;
x2.b=y;
}
main()
{
abc a1,a2;
clrscr();
cout<<"Enter the values for a1 object\n";
a1.read();
cout<<"\nEnter the values for a2 object\n";
a2.read();
swap(a1,a2);
cout<<"values of a1:\n";
a1.show();
cout<<"values of a2:\n";
a2.show();
getch();
}


/************<soeasyprograms.blogspot.in>***************/


Output of this program is:





No comments:

Post a Comment