Class Program 2

Write a C++ program to swap the value of private data members from 2 different classes.




#include<iostream>

using namespace std;
class xyz;
class abc
{
             int a;
public:
            void seta()
            {
                        a=5;
            }
            void printa()
            {
                        cout<<a<<endl;
            }
            friend void swap(abc &,xyz &);
};
class xyz
{
             int x;
public:
            void setx()
            {
                        x=10;
            }
            void printx()
            {
                        cout<<x<<endl;
            }
            friend void swap(abc &,xyz &);
};
void swap(abc &obj1,xyz &obj2)
{
            int t;
            t=obj1.a;
            obj1.a=obj2.x;
            obj2.x=t;
}
void main()
{
            abc o1;
            xyz o2;
            o1.seta();
            o2.setx();
            cout<<"\n Before swap";
            o1.printa();
            o2.printx();
            swap(o1,o2);
            cout<<"\n After swap";
            o1.printa();
            o2.printx();
}