Powered by Blogger.

Tuesday, February 5, 2013

swapping

/* Q.N 2. Write a program for swapping private data of two classes using friendly function and call by raference . */
#include<iostream.h>
#include<conio.h>

class class_2;
class class_1
{
      private:
              int x;
              public:
                     void getdata();
                     void display();
                     friend void swap(class_1 &,class_2 &);
                     };
                    
      void class_1::getdata()
      {
           cout<<"\nEnter A Value For X : ";
           cin>>x;
           }
          
      void class_1::display()
      {
           cout<<"\nThe Value Of X : "<<x;
           }
          
class class_2
{
      private:
              int y;
              public:
                     void getdata();
                     void display();
                     friend void swap(class_1 &,class_2 &);
                     };
                    
      void class_2::getdata()
      {
           cout<<"\nEnter A Value For Y : ";
           cin>>y;
           }
          
      void class_2::display()
      {
           cout<<"\nThe Value Of Y : "<<y;
           }

void swap(class_1 &a,class_2 &b)
{
     int temp;
     temp=a.x;
     a.x=b.y;
     b.y=temp;
     }

int main()
{
    class_1 p;
    class_2 q;
   
    p.getdata();
    q.getdata();
    cout<<"\n*******************************\n";
    cout<<"\nThe Values Before Swapping :\n";
    p.display();
    q.display();
    swap(p,q);
    cout<<"\n*******************************\n";
    cout<<"\nThe Values After Swapping :\n";
    p.display();
    q.display();
   
    getch();
    return 0;
}

0 comments:

Post a Comment