Answer to Question #227441 in C++ for Jim

Question #227441

1) Write a program to swap private data member of two classes. [The classes have no relation with each other]. 


2) Modify program 1) to make the swap function member of one class and friend to another class.


1
Expert's answer
2021-08-19T03:20:11-0400


2..

#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)
{
    cout << "\nBefore Swapping: " << s1.a << " " << s1.b;
    s1.temp = s1.a;
    s1.a = s1.b;
    s1.b = s1.temp;
    cout << "\nAfter Swapping: " << s1.a << " " << s1.b;
}
int main()
{
    Swap s(21, 18);
    swap(s);
    return 0;
}

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!

Comments

Jim
21.08.21, 19:20

Thanks a lot !!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS