-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphism.cpp
More file actions
185 lines (181 loc) · 3.28 KB
/
Polymorphism.cpp
File metadata and controls
185 lines (181 loc) · 3.28 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
#include <iostream>
#include <vector>
using namespace std;
class Shape{
public:
virtual void draw(){
cout << "draw shape" << endl;
}
};
class Circle: public virtual Shape{
public:
void draw(){
cout << "draw circle" << endl;
}
};
class Square: public virtual Shape{
public:
void draw(){
cout << "draw square" << endl;
}
};
class Triangle: public virtual Shape{
public:
void draw(){
cout << "draw triangle";
}
};
class Rodent{
public:
virtual void name() =0;
virtual ~Rodent()
{
cout << "deleted Rodent";
}
};
class Mouse: public Rodent{
public:
virtual void name(){
cout << "Mouse" << endl;
}
~Mouse()
{
cout << "deleted Mouse";
}
};
class Gerbil: public Rodent{
public:
virtual void name(){
cout << "Gerbil" << endl;
}
~Gerbil()
{
cout << "deleted Gerbil" << endl;
}
};
class Hamster: public Rodent{
public:
virtual void name(){
cout << "Gerbil" << endl;
}
~Hamster()
{
cout << "deleted Hamser" << endl;
}
};
class BlueHamster: public Hamster{
public:
virtual void name(){
cout << "Blue Hamster" << endl;
}
~BlueHamster()
{
cout << "deleted BlueHamster" << endl;
}
};
class AirCraft{
public:
virtual int getSpeed() = 0;
virtual void setSpeed(int n) = 0;
};
class Plane: public AirCraft{
private:
int num;
int speed;
public:
Plane(int n){
num =n;
speed = 0;
}
int virtual getSpeed(){
return speed;
}
void virtual setSpeed(int speed){
this->speed = speed;
}
virtual ~Plane()
{
cout << "Plane deleted" << endl;
}
};
class Helicopter: public AirCraft{
private:
int num;
int speed;
public:
Helicopter(int n){
num =n;
speed=0;
}
virtual int getSpeed(){
return speed;
}
virtual void setSpeed(int speed){
this->speed = speed;
}
virtual ~Helicopter()
{
cout << "Helicopter deleted" << endl;
}
};
class AirBaloon: public AirCraft{
private:
int speed;
public:
AirBaloon(int s){
speed =s;
}
int virtual getSpeed(){
return speed;
}
void virtual setSpeed(int speed){
this->speed = speed;
}
virtual ~AirBaloon()
{
cout << "Helicopter deleted" << endl;
}
};
class Tower{
vector<AirCraft*> aircraft;
public:
void add(AirCraft& aircrafts){
aircraft.push_back(&aircrafts);
}
void addSpeed(){
for(int i=0; i< aircraft.size(); i++){
aircraft[i]->setSpeed(i);
}
}
void getSpeed(){
for(int i=0; i< aircraft.size(); i++){
cout << aircraft[i]->getSpeed() << endl;
}
}
};
int main() {
/* Triangle t; t.draw();
Square s; s.draw();
vector<Rodent*> r;
r.push_back(new Hamster);
r.push_back(new Mouse);
r.push_back(new Gerbil);
r.push_back(new BlueHamster);
Rodent* tmp;
while(!r.empty()){
tmp = r.back();
tmp->name();
r.pop_back();
delete tmp;
} */
Tower tower;
Helicopter h(399);
Plane p(111);
Plane p2(112);
tower.add(p);
tower.add(h);
tower.add(p2);
tower.addSpeed();
tower.getSpeed();
return 0;
}