-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnap-CompoundObjects.cpp.html
303 lines (298 loc) · 7.96 KB
/
CodeSnap-CompoundObjects.cpp.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
<!----------------------------------------------------------------------------
CodeSnap-ClassAnatomy.cs.htm
Published 19 Mar 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-CompoundObjects</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-CompoundObjects.txt.html">N</a>
<a id="Prev" href="CodeSnap-CompoundObjects.txt.html">P</a>
<div style="position:fixed; top:150px; right:100px; opacity:0.85; width:35%;" onclick="this.style.display = 'none'">
<img src="Pictures/ObjectRelationships.JPG" width="98%" />
<div style="width:98%; text-align:center;">click to close</div>
</div>
<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-CompoundObjects.cpp.html">CompObj.cpp</a>,
<a href="CodeSnap-CompoundObjects.txt.html">CompObj.txt</a>,
<a class="disable" href="#">Code folder</a>,
<a href="CompoundObjects.htm">Compound Objects webpage</a>,
<a href="DesignNote-CppClassRelationships.htm">Class Relationships webpage</a>
</h3>
<div class="indent">
Compound objects are objects that contain and use instances of other classes.<br />
Of particular
importance is the way they are initialized in constructors.<br />
<br />
All of the classes in this demo have correct copy, assignment, destruction, and move semantics,
so we would normally defer to the compiler generated operations. However, for this
demo we want to show how each of these methods are defined and show when they are invoked
by writing from them to the console.
<spacer-15></spacer-15>
<a href="Pictures/ObjectRelationships.JPG">UML Diagram</a> showing an abstract view of this code.
</div>
<hr />
<pre class="codeSnap">
///////////////////////////////////////////////////////////////////////
// CompObj.cpp - Compound Object Operations //
// //
// Jim Fawcett, CSE687 - Object Oriented Design, Midterm Spring 2016 //
///////////////////////////////////////////////////////////////////////
/*
* The main objectives of this demonstrations are to:
* - illustrate the importance of constructor initialization sequences
* - expose all of the operations that compound objects are expected
* to provide.
*/
#include <iostream>
#include <string>
#include "../Utilities/Utilities.h"
void putMsg(const std::string& msg)
{
std::cout << "\n " << msg.c_str();
}
struct Cosmetic {
~Cosmetic() { std::cout << "\n\n"; }
} GlobalCosm;
///////////////////////////////////////////////////////////////////////
// C class - instances are composed by the base class B
class C
{
public:
C() { putMsg("C default construction"); }
C(const C& c) : name_(c.name_) // note initialization
{
putMsg("C copy construction");
}
C& operator=(const C& c)
{
putMsg("C assignment");
if (this == &c)
return *this;
name_ = c.name_;
return *this;
}
C(C&& c) : name_(std::move(c.name_))
{
putMsg("C move construction");
}
C& operator=(C&& c)
{
putMsg("C move assignment");
if (this == &c)
return *this;
name_ = std::move(c.name_);
return *this;
}
~C()
{
putMsg("C destruction");
}
private:
std::string name_;
};
///////////////////////////////////////////////////////////////////////
// B class - serves as the base of inheritance for D
/*
* B's constructors, as their first act, invoke a C constructor. They
* don't execute their bodies until C's construction completes.
*/
class B
{
public:
B()
{
putMsg("B default construction");
}
B(const B& b) : c(b.c)
{
putMsg("B copy construction");
}
B& operator=(const B& b)
{
putMsg("B copy assignment");
if (this == &b)
return *this;
c = b.c;
return *this;
}
B(B&& b) : c(std::move(b.c))
{
putMsg("B move construction");
}
B& operator=(B&& b)
{
putMsg("B move assignment");
if (this == &b)
return *this;
c = std::move(b.c);
return *this;
}
virtual void g()
{
putMsg("Calling g()");
}
virtual ~B()
{
putMsg("B destruction");
}
private:
C c;
};
///////////////////////////////////////////////////////////////////////
// U class - instances are used by D
class U
{
public:
U()
{
putMsg("U default construction");
}
U(const U& u)
{
putMsg("U copy construction");
}
U& operator=(const U& u)
{
putMsg("U assignment");
return *this;
}
U(U&& u)
{
putMsg("U move construction");
}
U& operator=(U&& u)
{
putMsg("U move assignment");
return *this;
}
~U()
{
putMsg("U destruction");
}
};
///////////////////////////////////////////////////////////////////////
// D class
// - D derives from B.
// - There are no virtual functions because our goal is to
// illustrate operations that occur when a compound object
// is created, assigned, and destroyed.
/*
* D's constructors, as their first act, invoke a B constructor. They
* don't execute their bodies until B's construction completes.
*/
class D : public B
{
public:
D()
{
putMsg("D default construction");
}
D(const D& d) : B(d)
{
putMsg("D copy construction");
}
D& operator=(const D& d)
{
putMsg("D assignment");
if (this == &d)
return *this;
B::operator=(d);
return *this;
}
D(D&& d) : B(std::move(d))
{
putMsg("D move construction");
}
D& operator=(D&& d)
{
putMsg("D move assignment");
if (this == &d)
return *this;
B::operator=(std::move(d));
return *this;
}
virtual void g()
{
putMsg("Calling D::g()");
}
void f(U& u)
{
putMsg("D using U");
}
~D()
{
putMsg("D destruction");
}
};
///////////////////////////////////////////////////////////////////////
// global function testFunction
// - Illustrates move construction from temporary objects.
D testFunction()
{
Utilities::title("Running testFunction");
D d;
return d; // d will be moved, not copied
}
///////////////////////////////////////////////////////////////////////
// Test stub
// - You should experiment with this and with the parts provided
// for the classes.
// - What do you think will happen if you comment out the move
// operations?
int main()
{
Utilities::title("Demonstrating Operation of Compound Object", '=');
U u;
D d;
d.f(u);
d = testFunction();
Utilities::title("starting copy construction");
D d2 = d;
Utilities::title("starting move construction");
D d3 = std::move(d);
// d is now invalid
Utilities::title("Demonstrating polymorphism");
B* ptr = new B();
ptr->g();
ptr = &d;
ptr->g();
Utilities::title("leaving main's scope");
}
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>