-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_allocation_and_polymorphism.cpp
81 lines (65 loc) · 1.21 KB
/
dynamic_allocation_and_polymorphism.cpp
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
#include <iostream>
using namespace std;
class Polygon
{
protected:
float width, height;
public:
Polygon(float a, float b): width(a), height(b)
{
}
virtual float area(void)= 0;
void print_area()
{
cout << this -> area() << endl;
}
};
class Rectangle: public Polygon
{
public:
Rectangle(float a, float b): Polygon(a,b)
{
cout << "This is a rectangle class" << endl;
}
float area()
{
return width * height;
}
};
class Triangle: public Polygon
{
public:
Triangle(float a, float b): Polygon(a,b)
{
cout << "This is a triangle class" << endl;
}
float area()
{
return width * height/2;
}
};
class Circle: public Polygon
{
public:
Circle(float a, float b): Polygon(a,b)
{
cout << "This is a circle class" << endl;
}
float area()
{
return 3.14 * (height / 2);
}
};
int main(int argc, char** argv)
{
Polygon *ppoly1 = new Rectangle(4,5);
Polygon *ppoly2 = new Triangle(4,5);
Polygon *ppoly3 = new Circle(0, 5);
ppoly1 -> print_area();
ppoly2 -> print_area();
ppoly3 -> print_area();
delete ppoly1;
delete ppoly2;
delete ppoly3;
return 0;
}