C++ program swapping of values between variables of a class using friend function
Answers
Answered by
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
Hindi,
6 months ago
Chemistry,
6 months ago
Social Sciences,
6 months ago
Physics,
1 year ago
Science,
1 year ago