-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSnap-Inheritance.cpp.html
More file actions
161 lines (148 loc) · 5.02 KB
/
CodeSnap-Inheritance.cpp.html
File metadata and controls
161 lines (148 loc) · 5.02 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
<!----------------------------------------------------------------------------
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>
<title>CodeSnap-Inheritance</title>
<script src="js/ScriptsUtilities.js"></script>
<script src="js/ScriptsTemplate.js"></script>
<script src="js/ScriptsKeyboard.js"></script>
<script src="js/ScriptsMenuCpp.js"></script>
<link rel="stylesheet" href="css/StylesTemplate.css" />
<link rel="stylesheet" href="css/StylesMenu.css" />
<style>
h3 {
font-weight: normal;
}
</style>
</head>
<body id="github" onload="initializeMenu()">
<nav>
<div id="navbar"></div>
</nav>
<a id="Next" href="CodeSnap-Inheritance.txt.html">N</a>
<a id="Prev" href="CodeSnap-Inheritance.h.html">P</a>
<navKeys-Container>
<nav-Key id="sKey" onclick="toggleSwipeEvents()">S</nav-Key>
<nav-Key id="rKey" onclick="location.reload()">R</nav-Key>
<nav-Key id="tKey" onclick="scrollPageTop()">T</nav-Key>
<nav-Key id="bKey" onclick="scrollPageBottom()">B</nav-Key>
<nav-Key id="hKey" onclick="helpWin()">H</nav-Key>
<nav-Key id="pKey" onclick="loadPrev()">P</nav-Key>
<nav-Key id="nKey" onclick="loadNext()">N</nav-Key>
</navKeys-Container>
<h3>
<a href="CodeSnap-Inheritance.h.html">Soldiers.h</a>,
<a href="CodeSnap-Inheritance.cpp.html">Soldiers.cpp</a>,
<a href="CodeSnap-Inheritance.txt.html">Soldiers.txt</a>,
<a class="disable" href="#">Code folder</a>,
<a href="Inheritance.htm">Inheritance webpage</a>
</h3>
<div class="indent">
This code illustrates how to build and use a polymorphic hierarchy of classes.
</div>
<hr />
<pre class="codeSnap">
/////////////////////////////////////////////////////////////////////////////////
// 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>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>