-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnap-Inheritance.html
335 lines (305 loc) · 8.49 KB
/
CodeSnap-Inheritance.html
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!----------------------------------------------------------------------------
CodeSnap-Inheritance.htm
Published 18 May 2017
Jim Fawcett, CSE687 : Object Oriented Design, Summer 2017
Note:
- Markup characters in the text part, enclosed in <pre>...</pre> have to be
replaced with escape sequences, e.g., < becomes < and > becomes >
- Be careful that you don't replace genuine markup characters with escape
sequences, e.g., everything outside of the <pre>...</pre> section.
----------------------------------------------------------------------------->
<html>
<head>
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/CSE687-LectNav.js"></script>
<script type="text/javascript" src="js/Fallback.js"></script>
<link rel="stylesheet" href="css/TopLevelV2.css?v=1.0" />
<link rel="stylesheet" href="css/CourseSupplements.css?v=1.0" />
<link rel="stylesheet" href="css/Fallback.css?v=1.0" />
<script type="text/javascript" src="js/CustomMarkup.js"></script>
<link rel="stylesheet" href="css/CustomMarkup.css?v=1.0" />
<style>
body {
margin: 0px 20px 20px 20px;
color: black;
background-color: #eee;
font-family: Consolas;
font-weight: 600;
font-size: 110%;
}
.indent {
margin-left: 20px;
margin-right: 20px;
}
h3 {
margin-bottom: 15px; margin-top: 15px;
}
h4 {
margin-bottom: 5px;
margin-top: 3px;
}
hr {
margin-top: 20px;
}
</style>
<style>
div.readings {
padding: 10px 20px 15px 20px;
}
div.readings > ul {
padding: 0px;
margin: 0px 20px;
}
div.readings > ul > li {
padding: 0px;
margin: 0px;
}
div.readings > ul > li > h4 {
padding: 0px;
margin: 0px;
}
pre {
font-family:"courier new", courier, monospace;
font-size:large;
}
</style>
</head>
<body>
<a name="top"></a>
<!-- site navigation menu built with Javascript -->
<nav>
<div id="nav">
<div id="remove">
<hr />
<a href="TopNav.htm">Site Navigation with no Javascript</a>
<hr />
<br />
</div>
</div>
</nav>
<div id="pagenav">
<ul>
<li><a href="#top">T</a></li>
<li><a id="N" href="#top">N</a></li>
<li><a id="P" href="#top" prev>P</a></li>
<li><a href="#bottom">B</a></li>
</ul>
</div>
<!--<h3>
<a href="CodeSnap-BasicHttpProgService.IService.cs.htm">IService.cs</a>,
<a href="CodeSnap-BasicHttpProgService.Service.cs.htm">Service.cs</a>,
<a href="CodeSnap-BasicHttpProgService.ProgClient.cs.htm">ProgClient.cs</a>,
<a href="CodeSnap-BasicHttpProgService.ProgHost.cs.htm">ProgHost.cs</a>
</h3>-->
<div style="font-size:large; font-weight:bold; margin:20px;">
This code illustrates how to build and use a polymorphic hierarchy of classes.
</div>
<hr />
<h3>Soldiers.h</h3>
<pre>
#ifndef SOLDIERS_H
#define SOLDIERS_H
/////////////////////////////////////////////////////////////////////////////////
// Soldiers.h - demonstrates inheritance //
// //
// Jim Fawcett, Summer Short Course, Su2016 //
/////////////////////////////////////////////////////////////////////////////////
/*
* Illustrates flexability afforded by polymorphic substitution.
* The global show function accepts a reference to any instance with
* class derived from Soldier, e.g., Officer, Sergeant, or Private.
* It then dynamically dispatches to the appropriate functions
* Soldier::salute() and Soldier::equipment().
*/
#include <string>
#include <vector>
#include <iostream>
#include "Utilities.h"
class Gear
{
public:
using Name = std::string;
Gear(const Name& name);
~Gear();
Name name();
protected:
Name name_;
};
class Person
{
public:
using Name = std::string;
Person(const Name& name);
void eat();
void sleep();
Name name();
virtual ~Person();
protected:
Name name_;
};
class Soldier : public Person
{
public:
using Equipment = std::vector<Gear>;
using Rank = std::string;
Soldier(const Name& name, const Rank& rank="private");
virtual void salute() = 0;
Rank rank() { return rank_; }
void add(const Gear& gear);
Equipment& equipment();
private:
Equipment equip_;
Rank rank_;
};
class Officer : public Soldier
{
public:
Officer(const Name& name, const Rank& rank="Captain");
virtual void salute();
};
class Sergeant : public Soldier
{
public:
Sergeant(const Name& name);
virtual void salute();
};
class Private : public Soldier
{
public:
Private(const Name& name);
virtual void salute();
};
void show(Soldier& s);
#endif
</pre>
<h3>Soldiers.cpp</h3>
<pre>
/////////////////////////////////////////////////////////////////////////////////
// Soldiers.cpp - demonstrates inheritance //
// //
// Jim Fawcett, Summer Short Course, Su2016 //
/////////////////////////////////////////////////////////////////////////////////
/*
* Illustrates flexability afforded by polymorphic substitution.
* The global show function accepts a reference to any instance with
* class derived from Soldier, e.g., Officer, Sergeant, or Private.
* It then dynamically dispatches to the appropriate functions
* Soldier::salute() and Soldier::equipment().
*/
#include "Soldiers.h"
Gear::Gear(const Name& name) : name_(name) {}
Gear::~Gear() {}
Gear::Name Gear::name() { return name_; }
Person::Person(const Name& name) : name_(name) {}
void Person::eat()
{
std::cout << "\n " << name() << " eating";
}
void Person::sleep()
{
std::cout << "\n " << name() << " sleeping";
}
Person::Name Person::name() { return name_; }
Person::~Person() {}
Soldier::Soldier(const Name& name, const Rank& rank) : Person(name), rank_(rank) {}
void Soldier::add(const Gear& gear)
{
equip_.push_back(gear);
}
Soldier::Equipment& Soldier::equipment()
{
return equip_;
}
Officer::Officer(const Name& name, const Rank& rank) : Soldier(name, rank) {}
void Officer::salute()
{
std::cout << "\n casual salute";
}
Sergeant::Sergeant(const Name& name) : Soldier(name, "Sergeant") {}
void Sergeant::salute()
{
std::cout << "\n default salute";
}
Private::Private(const Name& name) : Soldier(name) {}
void Private::salute()
{
std::cout << "\n snappy salute";
}
//----< global function accepts any Soldier object >-------
void show(Soldier& s)
{
s.salute();
std::cout << "\n Sir, my name is " << s.rank() << " " << s.name();
for (auto eq : s.equipment())
std::cout << "\n inspecting " << eq.name();
if (s.equipment().size() == 0)
std::cout << "\n Sorry sir, I forgot my equipment";
}
int main()
{
Officer off("Frank");
off.add(Gear("45 Caliber Pistol"));
Sergeant sgt("Sam");
sgt.add(Gear("m16 carbine"));
Private pvt1("George");
pvt1.add(Gear("m16 carbine"));
pvt1.add(Gear("40lb backpack"));
Private pvt2("Charlie");
pvt2.add(Gear("m16 carbine"));
pvt2.add(Gear("mortar"));
Private pvt3("Slacker");
Utilities::title("chow time");
off.eat();
sgt.eat();
pvt1.eat();
pvt2.eat();
pvt3.eat();
Utilities::putLine();
Utilities::title("inspection");
std::cout << "\n " << off.rank() << " " << off.name() << " inspecting his 1st squad";
show(sgt);
show(pvt1);
show(pvt2);
show(pvt3);
std::cout << "\n\n";
}
</pre>
<h3>Soldiers.txt</h3>
<pre>
chow time
-----------
Frank eating
Sam eating
George eating
Charlie eating
Slacker eating
inspection
------------
Captain Frank inspecting his 1st squad
default salute
Sir, my name is Sergeant Sam
inspecting m16 carbine
snappy salute
Sir, my name is private George
inspecting m16 carbine
inspecting 40lb backpack
snappy salute
Sir, my name is private Charlie
inspecting m16 carbine
inspecting mortar
snappy salute
Sir, my name is private Slacker
Sorry sir, I forgot my equipment
Press any key to continue . . .
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<footer>
<hr />
<div style="position:absolute; left:35px;">CSE687 - Object Oriented Design Course Notes</div>
Jim Fawcett © copyright 2017
</footer>
<a name="bottom"></a>
</body>
</html>