-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07b_oop_expanded.cpp
More file actions
343 lines (277 loc) · 11.6 KB
/
07b_oop_expanded.cpp
File metadata and controls
343 lines (277 loc) · 11.6 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// 07b_oop_expanded.cpp
//
// OOP expanded — builds directly on 07_oop_basics.cpp.
// Covers: encapsulation, inheritance, polymorphism,
// virtual functions, abstract classes, and operator overloading.
//
// Compile: g++ -std=c++17 -Wall -o 07b_oop_expanded 07b_oop_expanded.cpp
#include <iostream>
#include <string>
#include <vector>
#include <memory> // unique_ptr for polymorphism
#include <cmath> // std::sqrt
// ── 1. Encapsulation — private data, public interface ────────────────────────
class BankAccount {
private:
std::string _owner;
double _balance; // private — can't be accessed directly from outside
// Private helper — internal use only
void _log(const std::string &msg) const {
std::cout << " [" << _owner << "] " << msg << std::endl;
}
public:
// Constructor
BankAccount(const std::string &owner, double initial)
: _owner(owner), _balance(initial) {
_log("Account created with balance $" + std::to_string(initial));
}
// Destructor
~BankAccount() {
_log("Account closed");
}
// Getters — read-only access to private data
const std::string &owner() const { return _owner; }
double balance() const { return _balance; }
// Methods that validate before modifying
bool deposit(double amount) {
if (amount <= 0.0) {
_log("Deposit rejected — amount must be positive");
return false;
}
_balance += amount;
_log("Deposited $" + std::to_string(amount));
return true;
}
bool withdraw(double amount) {
if (amount <= 0.0) {
_log("Withdrawal rejected — amount must be positive");
return false;
}
if (amount > _balance) {
_log("Withdrawal rejected — insufficient funds");
return false;
}
_balance -= amount;
_log("Withdrew $" + std::to_string(amount));
return true;
}
void print() const {
std::cout << " Account[" << _owner << "] balance: $" << _balance << std::endl;
}
};
// ── 2. Inheritance ────────────────────────────────────────────────────────────
// Base class — represents any 2D shape
class Shape {
protected:
std::string _color; // protected — accessible in derived classes
public:
explicit Shape(const std::string &color) : _color(color) {}
// Virtual destructor — essential whenever using polymorphism
virtual ~Shape() {
std::cout << " Shape (" << _color << ") destroyed" << std::endl;
}
// Pure virtual — derived classes MUST implement these
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual std::string name() const = 0;
// Non-virtual — same for all shapes
const std::string &color() const { return _color; }
// Virtual — has a default, can be overridden
virtual void describe() const {
std::cout << " " << name() << " [" << _color << "]"
<< " area=" << area()
<< " perimeter=" << perimeter()
<< std::endl;
}
};
// Derived class — Circle
class Circle : public Shape {
private:
double _radius;
static constexpr double PI = 3.14159265358979;
public:
Circle(const std::string &color, double radius)
: Shape(color), _radius(radius) {}
~Circle() override {
std::cout << " Circle (r=" << _radius << ") destroyed" << std::endl;
}
double area() const override { return PI * _radius * _radius; }
double perimeter() const override { return 2.0 * PI * _radius; }
std::string name() const override { return "Circle"; }
double radius() const { return _radius; }
};
// Derived class — Rectangle
class Rectangle : public Shape {
private:
double _width;
double _height;
public:
Rectangle(const std::string &color, double width, double height)
: Shape(color), _width(width), _height(height) {}
~Rectangle() override {
std::cout << " Rectangle (" << _width << "x" << _height << ") destroyed" << std::endl;
}
double area() const override { return _width * _height; }
double perimeter() const override { return 2.0 * (_width + _height); }
std::string name() const override { return "Rectangle"; }
};
// Derived class — Triangle
class Triangle : public Shape {
private:
double _a, _b, _c; // three side lengths
public:
Triangle(const std::string &color, double a, double b, double c)
: Shape(color), _a(a), _b(b), _c(c) {}
double perimeter() const override {
return _a + _b + _c;
}
double area() const override {
// Heron's formula
double s = perimeter() / 2.0;
return std::sqrt(s * (s - _a) * (s - _b) * (s - _c));
}
std::string name() const override { return "Triangle"; }
};
// ── 3. Operator Overloading ───────────────────────────────────────────────────
class Vector2D {
public:
double x, y;
Vector2D(double x = 0.0, double y = 0.0) : x(x), y(y) {}
// + operator
Vector2D operator+(const Vector2D &other) const {
return Vector2D(x + other.x, y + other.y);
}
// - operator
Vector2D operator-(const Vector2D &other) const {
return Vector2D(x - other.x, y - other.y);
}
// * scalar multiply
Vector2D operator*(double scalar) const {
return Vector2D(x * scalar, y * scalar);
}
// == equality
bool operator==(const Vector2D &other) const {
return x == other.x && y == other.y;
}
// += compound assignment
Vector2D &operator+=(const Vector2D &other) {
x += other.x;
y += other.y;
return *this;
}
double length() const {
return std::sqrt(x * x + y * y);
}
// << stream output operator (non-member, defined as friend)
friend std::ostream &operator<<(std::ostream &os, const Vector2D &v) {
os << "(" << v.x << ", " << v.y << ")";
return os;
}
};
// ── 4. Multiple inheritance ───────────────────────────────────────────────────
class Flyable {
public:
virtual void fly() const {
std::cout << " " << creature_name() << " is flying" << std::endl;
}
virtual std::string creature_name() const = 0;
virtual ~Flyable() = default;
};
class Swimmable {
public:
virtual void swim() const {
std::cout << " " << creature_name() << " is swimming" << std::endl;
}
virtual std::string creature_name() const = 0;
virtual ~Swimmable() = default;
};
class Duck : public Flyable, public Swimmable {
public:
std::string creature_name() const override { return "Duck"; }
void quack() const {
std::cout << " Duck says: quack!" << std::endl;
}
};
int main() {
// ── 1. Encapsulation ──────────────────────────────────────────────────────
std::cout << "=== 1. Encapsulation ===" << std::endl;
BankAccount acc("Alice", 1000.0);
acc.deposit(500.0);
acc.withdraw(200.0);
acc.withdraw(5000.0); // rejected — insufficient
acc.withdraw(-50.0); // rejected — negative
acc.print();
// acc._balance = 99999; // ❌ would not compile — _balance is private
std::cout << " Balance via getter: $" << acc.balance() << std::endl;
// ── 2. Inheritance + virtual functions ────────────────────────────────────
std::cout << "\n=== 2. Inheritance ===" << std::endl;
Circle c("red", 5.0);
Rectangle r("blue", 4.0, 6.0);
Triangle t("green", 3.0, 4.0, 5.0);
c.describe();
r.describe();
t.describe();
// ── 3. Polymorphism ───────────────────────────────────────────────────────
std::cout << "\n=== 3. Polymorphism ===" << std::endl;
// Base class pointer — points to different derived types
// unique_ptr used here — no manual delete needed
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Circle>("orange", 3.0));
shapes.push_back(std::make_unique<Rectangle>("purple", 5.0, 2.0));
shapes.push_back(std::make_unique<Triangle>("cyan", 6.0, 8.0, 10.0));
shapes.push_back(std::make_unique<Circle>("yellow", 1.0));
std::cout << " All shapes via base pointer:" << std::endl;
double total_area = 0.0;
for (const auto &shape : shapes) {
shape->describe(); // calls correct derived describe()
total_area += shape->area();
}
std::cout << " Total area: " << total_area << std::endl;
// Find largest shape
const Shape *largest = nullptr;
double max_area = 0.0;
for (const auto &shape : shapes) {
if (shape->area() > max_area) {
max_area = shape->area();
largest = shape.get();
}
}
std::cout << " Largest: ";
largest->describe();
// ── 4. Operator overloading ───────────────────────────────────────────────
std::cout << "\n=== 4. Operator Overloading ===" << std::endl;
Vector2D v1(3.0, 4.0);
Vector2D v2(1.0, 2.0);
std::cout << " v1 = " << v1 << std::endl;
std::cout << " v2 = " << v2 << std::endl;
std::cout << " v1 + v2 = " << (v1 + v2) << std::endl;
std::cout << " v1 - v2 = " << (v1 - v2) << std::endl;
std::cout << " v1 * 2.0 = " << (v1 * 2.0) << std::endl;
std::cout << " v1 == v2? " << (v1 == v2 ? "yes" : "no") << std::endl;
std::cout << " v1 == v1? " << (v1 == v1 ? "yes" : "no") << std::endl;
std::cout << " v1.length() = " << v1.length() << std::endl;
v1 += v2;
std::cout << " v1 after += v2: " << v1 << std::endl;
// ── 5. Multiple inheritance ───────────────────────────────────────────────
std::cout << "\n=== 5. Multiple Inheritance ===" << std::endl;
Duck duck;
duck.fly();
duck.swim();
duck.quack();
// Polymorphism through interface pointers
Flyable *f = &duck;
Swimmable *s = &duck;
f->fly();
s->swim();
// ── 6. Key OOP concepts summary ───────────────────────────────────────────
std::cout << "\n=== 6. OOP Concepts Summary ===" << std::endl;
std::cout << " Encapsulation — private data, public interface via methods" << std::endl;
std::cout << " Inheritance — derived class extends base class" << std::endl;
std::cout << " Polymorphism — base pointer calls derived method at runtime" << std::endl;
std::cout << " Abstraction — pure virtual = interface contract" << std::endl;
std::cout << " virtual — enables runtime dispatch (polymorphism)" << std::endl;
std::cout << " override — confirms method overrides a virtual" << std::endl;
std::cout << " virtual ~dtor — always use with polymorphic base classes" << std::endl;
std::cout << "\n(Shapes destroyed as unique_ptrs go out of scope)" << std::endl;
return 0;
}