1) Write a program to swap private data member of two classes. [The classes have no relation with each other].
2) Modify program 1) to make the swap function member of one class and friend to another class.
2..
#include <iostream>
using namespace std;
class Swap {
int temp, a, b;
public:
Swap(int a, int b)
{
this->a = a;
this->b = b;
}
friend void swap(Swap&);
};
void swap(Swap& s1)
{
cout << "\nBefore Swapping: " << s1.a << " " << s1.b;
s1.temp = s1.a;
s1.a = s1.b;
s1.b = s1.temp;
cout << "\nAfter Swapping: " << s1.a << " " << s1.b;
}
int main()
{
Swap s(21, 18);
swap(s);
return 0;
}
Comments
Thanks a lot !!
Leave a comment