-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_21.cpp
More file actions
46 lines (37 loc) · 932 Bytes
/
Problem_21.cpp
File metadata and controls
46 lines (37 loc) · 932 Bytes
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
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual void calculateArea() = 0; // Pure virtual function
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
void calculateArea() override {
double area = width * height;
cout << "Rectangle area: " << area << endl;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
void calculateArea() override {
double area = M_PI * radius * radius;
cout << "Circle area: " << area << endl;
}
};
int main() {
Shape* shape;
Rectangle rect(5, 7);
Circle circ(3);
shape = ▭
shape->calculateArea(); // Calls Rectangle's calculateArea
shape = ˆ
shape->calculateArea(); // Calls Circle's calculateArea
return 0;
}