Exchange private values of two classes with friend function

Q. Write a C++ program to demonstrate how a common friend function can be used to exchange the private values of two classes. (Use call by reference method).

Answer:

Following program is demonstrating how a common friend function can be used to exchange the private values of two classes using call-by-reference method.

#include<iostream>
using namespace std;
class Derived;
class Base
{
    protected:
        int num1;
    public:
        Base()
        {
                num1=10;
        }
        void show()
        {
                cout<<"\n Value of Number 1 : "<<num1;
        }
        friend void swap(Base *num1, Derived *num2);
};
class Derived
{
    protected:
        int num2;
    public:
        Derived()
        {
                num2=20;
        }
        void show()
        {
                cout<<"\n Value of Number 2 : "<<num2;
        }
        friend void swap(Base *num1, Derived *num2);
};
void swap(Base *no1, Derived *no2)
{
        int no3;
        no3=no1->num1;
        no1->num1=no2->num2;
        no2->num2=no3;
}
int main()
{
        Base b;
        Derived d;
        swap(&b, &d);
        b.show();
        d.show();
        return 0;
}


Output:

common friend function