Tuesday, 22 November 2016

Swapping Two Class Members Using Friend Function

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class second;
class first
{
  int a;
 public:
    void get()
 {
    cout<<"Enter Value For A : ";
    cin>>a;
 }
  friend void swap(first f,second s)   
};
class second
{
  int b;
 public:
    void get()
 {
    cout<<"Enter Value For B : ";
    cin>>b;
 }
  friend void swap(first f,second s)   
};
void swap(first f,second s)
{
  int t;
  t=f.a;
  f.a=f.b;
  s.b=t;
  cout<<"Swapping Two Class Members"<<endl;
  cout<<"------------ ----- -------------------"<<endl;
  cout<<"Before Swapping"<<endl;
  cout<<"-------- ------------"<<endl;
    cout<<"First Class Member: "<<f.a<<endl;
    cout<<"Second Class Member: "<<s.b<<endl;
  cout<<"After Swapping"<<endl;
  cout<<"-------------------"<<endl;
    cout<<"First Class Member: "<<s.b<<endl;
    cout<<"Second Class Member: "<<f.a<<endl;
}
void main()
{
  clrscr();
  first f;
  f.get();
  second s;
  s.get();
  swap(f,s);
 getch();
}

No comments:

Post a Comment