Write a program using friend function to swap the private data of two classes assuming each class to contain one private integer data member and associated member functions for inputting and displaying the data.
#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)
{
s1.temp = s1.a;
s1.a = s1.b;
s1.b = s1.temp;
}
Comments
Leave a comment