-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquiz04.tex
More file actions
568 lines (444 loc) · 9.41 KB
/
quiz04.tex
File metadata and controls
568 lines (444 loc) · 9.41 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
\documentclass[landscape]{slides}
\usepackage[margin=.5in]{geometry}
\usepackage{tabularx}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}
\usepackage{graphics}
\usepackage[english]{babel}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\lstset{basicstyle={\tiny\ttfamily},backgroundcolor=\color{hellgelb},frame=single,numbers=left,numberstyle={\tiny\sffamily}}
\newcommand{\st}{Slide Title}
\onlyslides{0-999}
\begin{document}
%%%%%
\renewcommand{\st}{Concrete types (or value types)}
\begin{slide}
\begin{lstlisting}
class Date {
int d, m, y;
public:
Date(int day, int month, int year) {
d = day;
m = month;
y = year;
}
int day() {
return d;
}
int month() {
return this->m;
}
int year() {
return y;
}
};
\end{lstlisting}
Please criticize this code.
\begin{note}
\st
\begin{tiny}
There are several things to comment here:
\begin{itemize}
\item probably not a good idea to define several variables on line 2
\item not using member initializer properly
\item possible confusion about day, day(), and d
\item a const after the argument list indicates that these functions do no modify the state of a Date object (query method)
\item a non-const methods indicates that the state of an object changes (modifier method)
\item how to handle invalid Date?
\item do we need copy constructor and assignment operator?
\item the use of this-> on line 13 is not necessary, but some prefer to write this-> for clearity.
\item it often makes sence to place data members last to empasize the funtions providing the public user interface [p234]
\end{itemize}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{mutable, name collisions}
\begin{slide}
\tiny{foo.hpp}
\begin{lstlisting}
#include <iostream>
class Foo {
int value;
int read_counter;
public:
Foo(int v) : value(v), read_counter(0) { }
int value();
void debug_print_read_counter();
};
\end{lstlisting}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lr}
\begin{minipage}[t]{.52\linewidth}
\tiny{foo.cpp}
\begin{lstlisting}
#include "foo.hpp"
int Foo::value() const {
read_counter++;
return value;
}
void Foo::debug_print_read_counter() {
std::cerr << "read_counter = "
<< read_counter << std::endl;
}
\end{lstlisting}
\end{minipage}
&
\begin{minipage}[t]{.43\linewidth}
\tiny{main.cpp}
\begin{lstlisting}
#include "foo.hpp"
int main() {
Foo f(42);
f.debug_print_read_counter();
f.value();
f.value();
f.debug_print_read_counter();
}
\end{lstlisting}
\end{minipage}
\end{tabular*}
\
\begin{lstlisting}[numbers=none]
g++ foo.cpp main.cpp && ./a.out
\end{lstlisting}
This code does not compile and it smells bad. How can you fix it? Please criticize.
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
- iosfwd
- conflicting redeclaration of Foo::value() and Foo::value
- consider underscore postfixing of member variables
- declaration on foo.hpp:8 and definition on foo.cpp:3 does not match
- logical constness (p241). consider making read_counter mutable
- debug_print_read_counter should be const, indicating that it is a query
- there is a cohesion problem with Foo.cpp
\end{verbatim}
Once fixed, it might print:
\begin{verbatim}
read_counter = 0
read_counter = 2
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{new[]/delete[], the rule of three}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct B {
B() { p('B'); }
~B() { p('b'); }
};
class A {
B * v;
public:
A() { p('A'); v = new B[3]; }
~A() { p('a'); delete v; }
};
int main() {
A a1;
A a2 = a1;
A a3;
a3 = a1;
}
\end{lstlisting}
What might happen if you try to compile, link and run this program? Please criticize.
\begin{note}
\st
\begin{tiny}
On my machine I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
ABBBABBBababab
\end{verbatim}
\begin{itemize}
\item if you new an array you must delete an array
\item the rule of three, if you need a destructor, you probably need a copy constructor and copy assignment operator
\item deleting something twice is a serious error; the behaviour is undefined and most likely disastrous
\item should the destructor of A be virtual?
\end{itemize}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{initialization of objects}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct A {
A() { p('A'); }
~A() { p('a'); }
};
struct B {
B() { p('B'); }
~B() { p('b'); }
};
class C {
A a;
B b;
public:
C() : b(), a() {}
C(int) {}
};
int main() {
C c1;
C c2(4);
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
On my machine I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp: In constructor 'C::C()':
scratch.cpp:17: warning: 'C::b' will be initialized after
scratch.cpp:16: warning: 'A C::a'
scratch.cpp:19: warning: when initialized here
ABABbaba
\end{verbatim}
Without -Wall, no warning is printed. \
If a member constructor needs no argument, the member need not to be mentioned in the member initializer list.
\
what about native type members?
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{using member initializer}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct A {
int value;
A() { p(0); }
A(int v) : value(v) { p(1); }
~A() { p(2); }
A(const A & a) { p(3); }
void operator=(const A & a) { p(4); }
};
struct B {
A a;
B(int v) { p('B'); a=42; p('b'); }
};
struct C {
A a;
C(int v) : a(v) { p('C'); p('c'); }
};
int main() {
p('-');
B b(42);
p('-');
C c(42);
p('-');
}
\end{lstlisting}
What might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
-0B142b-1Cc-22
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{placement}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct X {
int a;
int b;
X() : a(1), b(2) {}
};
struct Y {
int c;
int d;
Y() : c(3), d(4) {}
};
int main() {
X x;
new (&x) Y;
p(x.a);
p(x.b);
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
34
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{temporary objects}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct X {
int value;
X(int v) : value(v) { p('X'); p(v); }
~X() { p('x'); }
X(const X & x) : value(x.value) { p(2); }
X operator+(const X & x) { p(3); return this->value + x.value; }
};
int main() {
X a(4);
p('-');
p( (a + a).value );
p('-');
X b = (a + a);
p('-');
p(b.value);
p('-');
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
X4-3X88x-3X8-8-xx
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{implicit conversion}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct X {
X(int v) { p('X'); p(v); }
~X() { p('x'); }
};
void foo(X x) {
p('F');
}
int main() {
foo(4);
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
X4Fx
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{conversion operator}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
struct X {
int value;
X(int v) : value(v) { }
operator int() const { return value; }
};
void foo(int v) {
p(v);
}
int main() {
X x = 42;
foo(x);
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
42
\end{verbatim}
What happens if you put explicit in front of line 7? What would you then need to write on line 16?
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{functor}
\begin{slide}
\begin{lstlisting}
#include <iostream>
#include <vector>
class X {
int value;
public:
X(int v) : value(v) {}
void operator()(int & i) const {
i += value;
}
};
void p(int & i) {
std::cout << i << std::endl;
}
int main() {
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
for_each(v.begin(), v.end(), X(42));
for_each(v.begin(), v.end(), p);
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
43
44
45
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
%% template
%% \renewcommand\st{§x.x, title}
%% \begin{slide}
%%
%% \begin{lstlisting}
%%
%% \end{lstlisting}
%%
%% what might happen if you try to compile, link and run this program?
%%
%% \begin{note}
%% \st
%% \begin{tiny}
%% \begin{verbatim}
%%
%% \end{verbatim}
%%
%% \end{tiny}
%% \end{note}
%%
%% \end{slide}
\end{document}