-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.cpp
More file actions
131 lines (102 loc) · 2.24 KB
/
polymorphism.cpp
File metadata and controls
131 lines (102 loc) · 2.24 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
#include<iostream>
using namespace std;
class Polynomial {
protected:
int num_sides;
float sum_of_interior_angles;
void calc_sum_of_interior_angles();
public:
Polynomial(int);
int get_num_sides();
void set_num_sides(int n);
float get_sum_of_interior_angles();
virtual float get_area();
};
Polynomial::Polynomial(int num_sides){
this->num_sides = num_sides;
calc_sum_of_interior_angles();
}
int Polynomial::get_num_sides(){return num_sides;}
void Polynomial::set_num_sides(int n){
num_sides = n;
calc_sum_of_interior_angles();
}
void Polynomial::calc_sum_of_interior_angles(){
sum_of_interior_angles = (num_sides - 2) * 180;
}
float Polynomial::get_sum_of_interior_angles(){return sum_of_interior_angles;}
float Polynomial::get_area(){
return -1;
}
class Rectangle:public Polynomial{
protected:
float area;
float l, b;
void calc_area();
public:
Rectangle(float, float);
void set_length(float);
void set_breadth(float);
float get_length();
float get_breadth();
float get_area();
};
Rectangle::Rectangle(float l, float b) : Polynomial(4){
this->l = l;
this->b = b;
calc_area();
}
void Rectangle::calc_area(){
area = l * b;
}
void Rectangle::set_length(float l){
this->l = l;
calc_area();
}
void Rectangle::set_breadth(float b){
this->b = b;
calc_area();
}
float Rectangle::get_length(){return l;}
float Rectangle::get_breadth(){return b;}
float Rectangle::get_area(){return area;}
class Triangle:public Polynomial{
protected:
float area;
float base, height;
void calc_area();
public:
Triangle(float, float);
void set_base(float);
void set_height(float);
float get_base();
float get_height();
float get_area();
};
Triangle::Triangle(float base, float height) : Polynomial(3){
this->base = base;
this->height = height;
calc_area();
}
void Triangle::calc_area(){
area = 0.5 * base * height;
}
void Triangle::set_base(float base){
this->base = base;
calc_area();
}
void Triangle::set_height(float height){
this->height = height;
calc_area();
}
float Triangle::get_base(){return base;}
float Triangle::get_height(){return height;}
float Triangle::get_area(){return area;}
int main(){
Polynomial *p;
Rectangle r(3, 4);
Triangle t(4, 4);
p = & r;
cout<< p -> get_area();
return 0;
}