-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeSnap-CompoundObjects.html
415 lines (403 loc) · 9.77 KB
/
CodeSnap-CompoundObjects.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!----------------------------------------------------------------------------
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>
<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;">
Compound objects are objects that contain and use instances of other classes. Of particular
importance is the way they are initialized in constructor implementations. That aspect is
illustrated in this example.
<p></p>
<a href="ObjectRelationships.jpg">UML Diagram</a> showing an abstract view of this code.
</div>
<hr />
<h3>CompObj.cpp</h3>
<pre>
///////////////////////////////////////////////////////////////////////
// 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>
<h3>CompObj.txt</h3>
<pre>
============================================
Demonstrating Operation of Compound Object
============================================
U default construction
C default construction
B default construction
D default construction
D using U
----------------------
Running testFunction
----------------------
C default construction
B default construction
D default construction
C move construction
B move construction
D move construction
D destruction
B destruction
C destruction
D move assignment
B move assignment
C move assignment
D destruction
B destruction
C destruction
----------------------------
starting copy construction
----------------------------
C copy construction
B copy construction
D copy construction
----------------------------
starting move construction
----------------------------
C move construction
B move construction
D move construction
----------------------------
Demonstrating polymorphism
----------------------------
C default construction
B default construction
Calling B::g()
Calling D::g()
----------------------
leaving main's scope
----------------------
D destruction
B destruction
C destruction
D destruction
B destruction
C destruction
D destruction
B destruction
C destruction
U destruction
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>