-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop_lab3.cpp
More file actions
195 lines (142 loc) · 3.15 KB
/
oop_lab3.cpp
File metadata and controls
195 lines (142 loc) · 3.15 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
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <string>
using namespace std;
class Machinery
{
string _name;
int _weight;
public:
Machinery (string s, int w) : _name(s), _weight(w) {}
virtual ~Machinery () { cout << "Machinery::~Machinery" << endl; }
int weight () { return _weight; }
virtual void operate (int hours = 1) = 0;
};
class Vehicle : public Machinery
{
public:
Vehicle (string s, int w) : Machinery(s, w) {}
void operate (int hours = 3) { cout << "Vehicle operating for " << hours << " hour(s)" << endl; }
};
class Computer : public Machinery
{
public:
Computer (string s, int w) : Machinery(s, w) {}
~Computer () { cout << "Computer::~Computer" << endl; }
void operate (int hours = 5) { cout << "Computer operating for " << hours << " hour(s)" << endl; }
};
//////////////////////////////////////////////////
class Alive
{
int _age;
int _health;
public:
Alive (int h) : _age(0), _health(h) {}
virtual ~Alive() {}
int age () { return _age; }
int health () { return _health; }
bool dead () { return _health < 0; }
void eat () { cout << "Mmm" << endl; }
virtual bool getOld ()
{
cout << "Alive::getOld" << endl;
if ( dead() ) return false;
_age++; _health -= 5;
return true;
}
};
class Person : public Alive
{
string _name;
int _height;
Computer* pc;
// Test in combination with virtual in ~Machinery() and ~Alive()
//Machinery* pc;
public:
Person (string s) : Alive(200), _name(s), _height(50), pc(NULL) {}
virtual ~Person () { if ( pc ) delete pc; }
string& name () { return _name; }
int height () { return _height; }
void buyPC () { pc = new Computer("my computer", 5); }
void eat () { cout << "Eating my food" << endl; }
bool getOld ()
{
cout << "Person::getOld" << endl;
if ( !Alive::getOld() ) return false;
if ( age() > 50 )
_height--;
else
_height++;
return true;
}
virtual void workOn (Machinery* i)
{
cout << "Generic operation on machinery" << endl;
i->operate();
}
virtual void workOn (Vehicle* v)
{
cout << "Generic operation on vehicle" << endl;
v->operate();
}
};
class Technician : public Person
{
int _money;
public:
Technician (string s) : Person(s), _money(0) {}
int money () { return _money; }
bool getOld ()
{
cout << "Technician::getOld" << endl;
if ( !Person::getOld() ) return false;
_money++;
return true;
}
void workOn (Machinery* i)
{
cout << "Special operation on machinery" << endl;
i->operate();
}
void workOn (Computer* c)
{
cout << "Special operation on computer" << endl;
c->operate();
}
};
int main ()
{
Alive a(100);
a.getOld();
Person p("george");
p.getOld();
Technician t("mastro-giorgos");
t.getOld();
Alive* aPtr = &a;
aPtr->getOld();
aPtr = &p;
aPtr->getOld();
aPtr = &t;
aPtr->getOld();
Alive& aRef = p;
aRef.getOld();
aPtr->eat();
t.eat();
aPtr = new Person("Jamez");
((Person*)aPtr)->buyPC();
delete aPtr;
Vehicle c("car", 1000);
Computer pc("pc", 5);
Machinery* mP = &c;
Person* pPtr = &p;
pPtr->workOn(mP);
pPtr->workOn(&c);
pPtr->workOn(&pc);
pPtr = &t;
pPtr->workOn(mP);
pPtr->workOn(&c);
pPtr->workOn(&pc);
Technician* tPtr = &t;
tPtr->workOn(mP);
tPtr->workOn(&c);
tPtr->workOn(&pc);
}