Question #274614

Write a program using friend function to swap the private data of two classes assuming each class to contain one private integer data member and associated member functions for inputting and displaying the data.

 



Expert's answer

#include <iostream>
using namespace std;

class Swap {

    int temp, a, b;

public:
    Swap(int a, int b)
    {
        this->a = a;
        this->b = b;
    }

    friend void swap(Swap&);
};


void swap(Swap& s1)
{
    s1.temp = s1.a;
    s1.a = s1.b;
    s1.b = s1.temp;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS