-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_13.cpp
More file actions
58 lines (53 loc) · 1.14 KB
/
Problem_13.cpp
File metadata and controls
58 lines (53 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
class class_2; // Forward declaration
class class_1 {
int num1;
int num2;
public:
class_1(int num1, int num2) {
this->num1 = num1;
this->num2 = num2;
}
void display() {
cout << "Display Class_1" << endl;
cout << num1 << endl;
cout << num2 << endl;
}
friend void swap(class_1 &, class_2 &);
};
class class_2 {
int num1;
int num2;
public:
class_2(int num1, int num2) {
this->num1 = num1;
this->num2 = num2;
}
void display() {
cout << "Display Class_2" << endl;
cout << num1 << endl;
cout << num2 << endl;
}
friend void swap(class_1 &, class_2 &);
};
void swap(class_1 &c1, class_2 &c2) {
int temp1 = c1.num1;
int temp2 = c1.num2;
c1.num1 = c2.num1;
c1.num2 = c2.num2;
c2.num1 = temp1;
c2.num2 = temp2;
}
int main() {
class_1 c1(10, 20);
class_2 c2(30, 40);
cout << "Display Before Swap..." << endl;
c1.display();
c2.display();
swap(c1, c2);
cout << "Display After Swap..." << endl;
c1.display();
c2.display();
return 0;
}