/*
Friend Function of one class as a friend of another class
Swap two numbers
*/
#include<iostream>
using namespace std;
class Demo;
class Sample
{
int a;
public:
void accept()
{
cout<<"\n\n Enter any number: ";
cin>>a;
}
void display()
{
cout<<"\n A:"<<a;
}
void swap(Demo &D);
};
class Demo
{
int b;
public:
void accept()
{
cout<<"\n\n Enter any number: ";
cin>>b;
}
void display()
{
cout<<"\n B:"<<b;
}
friend void Sample:: swap(Demo &D);
};
void Sample::swap(Demo &D)
{
int temp;
temp=a;
a=D.b;
D.b=temp;
}
int main()
{
Sample s;
Demo d;
s.accept();
d.accept();
cout<<"\n\n Before Swap:";
s.display();
d.display();
s.swap(d);
cout<<"\n\n After Swap:";
s.display();
d.display();
}
Friend Function of one class as a friend of another class
Swap two numbers
*/
#include<iostream>
using namespace std;
class Demo;
class Sample
{
int a;
public:
void accept()
{
cout<<"\n\n Enter any number: ";
cin>>a;
}
void display()
{
cout<<"\n A:"<<a;
}
void swap(Demo &D);
};
class Demo
{
int b;
public:
void accept()
{
cout<<"\n\n Enter any number: ";
cin>>b;
}
void display()
{
cout<<"\n B:"<<b;
}
friend void Sample:: swap(Demo &D);
};
void Sample::swap(Demo &D)
{
int temp;
temp=a;
a=D.b;
D.b=temp;
}
int main()
{
Sample s;
Demo d;
s.accept();
d.accept();
cout<<"\n\n Before Swap:";
s.display();
d.display();
s.swap(d);
cout<<"\n\n After Swap:";
s.display();
d.display();
}