-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolimorphismExample.cpp
More file actions
48 lines (40 loc) · 818 Bytes
/
PolimorphismExample.cpp
File metadata and controls
48 lines (40 loc) · 818 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
47
48
#include <iostream>
using namespace std;
class Shape {
protected:
double width;
double height;
public:
void set_dimensions(double w, double h) {
width = w;
height = h;
}
virtual double area() {
cout << "Area of shape not defined." << endl;
return 0.0;
}
};
class Rectangle : public Shape {
public:
double area() {
return width * height;
}
};
class Triangle : public Shape {
public:
double area() {
return 0.5 * width * height;
}
};
int main() {
Shape* shape;
Rectangle rect;
Triangle tri;
shape = ▭
shape->set_dimensions(5, 10);
cout << "Rectangle area: " << shape->area() << endl;
shape = &tri;
shape->set_dimensions(5, 10);
cout << "Triangle area: " << shape->area() << endl;
return 0;
}