Computer Science, asked by vidhe1434, 11 months ago

C++ program swapping of values between variables of a class using friend function

Answers

Answered by sushiladevi4418
0

Answer:

C++ program swapping of values between variables of a class using friend function

Explanation:

#include<iostream>

using namespace std;  

class Test {

private:

   int x, y, temp;

public:  

   void input() {

       cout << "Enter Two Numbers :";

       cin >> x>>y;

   }  

   friend void swap(Test &t);  

   void display() {

       cout << "\nAfter Swap x is :" << x;

       cout << "\nAfter Swap y is :" << y;

   }

}

void swap(Test &t) {

   t.temp = t.x;

   t.x = t.y;

   t.y = t.temp;  

}  

int main() {  

   Test t;

   t.input();  

   swap(t);

   t.display();  

   return 0;

}

Output: -

Enter Two Numbers :50 60

After Swap x is: 60

After Swap y is: 50

Similar questions