-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp6.cpp
More file actions
210 lines (156 loc) · 3.4 KB
/
Exp6.cpp
File metadata and controls
210 lines (156 loc) · 3.4 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//Inheritance
#include <iostream>
class A {
private:
int x;
public:
A(int x) : x(x) {}
void swap(A& other) {
std::swap(x, other.x);
}
void print() {
std::cout << "x: " << x << std::endl;
}
};
class B : public A {
private:
int y;
public:
B(int x, int y) : A(x), y(y) {}
void swap(B& other) {
A::swap(other); // Swaps the `A` portion of `this`.
std::swap(y, other.y); // Swaps the `B` portion of `this`.
}
void print() {
A::print();
std::cout << "y: " << y << std::endl;
}
};
int main() {
A a(10);
B b(20, 30);
std::cout << "Before swapping:" << std::endl;
a.print();
b.print();
a.swap(b);
std::cout << "After swapping:" << std::endl;
a.print();
b.print();
return 0;
}
//Exception Handling
#include <iostream>
using namespace std;
class InvalidSwapException : public exception {
public:
InvalidSwapException() {
cout << "Invalid swap operation attempted." << endl;
}
};
class Data {
private:
int value;
public:
Data(int value) : value(value) {}
void swap(Data& other) {
if (other.value == value) {
throw InvalidSwapException();
}
int temp = value;
value = other.value;
other.value = temp;
}
void print() const {
cout << "Value: " << value << endl;
}
};
int main() {
try {
Data a(10);
Data b(20);
cout << "Before swapping:" << endl;
a.print();
b.print();
a.swap(b);
cout << "After swapping:" << endl;
a.print();
b.print();
} catch (InvalidSwapException& e) {
cout << e.what() << endl;
}
return 0;
}
//Friend Function
#include <iostream>
class Data {
private:
int value;
public:
Data(int value) : value(value) {}
friend void swap(Data& a, Data& b) {
int temp = a.value;
a.value = b.value;
b.value = temp;
}
void print() const {
std::cout << "Value: " << value << std::endl;
}
};
int main() {
Data a(10);
Data b(20);
std::cout << "Before swapping:" << std::endl;
a.print();
b.print();
swap(a, b);
std::cout << "After swapping:" << std::endl;
a.print();
b.print();
return 0;
}
//Virtual Function
#include <iostream>
class Data {
private:
int value;
public:
Data(int value) : value(value) {}
virtual void swapValues(Data& other) = 0;
void print() const {
std::cout << "Value: " << value << std::endl;
}
};
class DataImpl : public Data {
public:
DataImpl(int value) : Data(value) {}
void swapValues(Data& other) override {
int temp = value;
value = other.value;
other.value = temp;
}
};
class DataDerived : public Data {
public:
DataDerived(int value) : Data(value) {}
void swapValues(Data& other) override {
// Implement custom swapping logic for derived data
std::cout << "Swapping values in DataDerived" << std::endl;
int temp = value;
value = other.value;
other.value = temp;
}
};
int main() {
DataImpl a(10);
DataDerived b(20);
std::cout << "Before swapping:" << std::endl;
a.print();
b.print();
Data& d1 = a; // Use base class reference for swapping
Data& d2 = b; // Use base class reference for swapping
d1.swapValues(d2);
std::cout << "After swapping:" << std::endl;
a.print();
b.print();
return 0;
}