-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquiz09.tex
More file actions
459 lines (350 loc) · 8.2 KB
/
quiz09.tex
File metadata and controls
459 lines (350 loc) · 8.2 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
\documentclass[landscape]{slides}
\usepackage[margin=.5in]{geometry}
\usepackage{tabularx}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{color}
\usepackage{graphics}
\usepackage[english]{babel}
\usepackage{underscore}
\definecolor{hellgelb}{rgb}{1,1,0.8}
\lstset{basicstyle={\tiny\ttfamily},backgroundcolor=\color{hellgelb},frame=single,numbers=left,numberstyle={\tiny\sffamily},firstnumber=auto}
\newcommand{\st}{Slide Title}
\onlyslides{0-999}
\begin{document}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
class Rectangle {
public:
Rectangle(int width, int height) : width_(width), height_(height) {}
int width() { return width_; }
int height() { return height_; }
int area() { return width() * height(); }
private:
int width_;
int height_;
};
class Square : public Rectangle {
public:
Square(int length) : Rectangle(length,length) {}
};
int main() {
Square s(4);
std::cout << s.area() << std::endl;
}
\end{lstlisting}
When will be printed when running this code? Please critizise the design.
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
16
\end{verbatim}
When finding classes: the classification should be of aspects of the
concepts that we model in our system, rather than aspects that may be
valid in other areas. [p703]
Inheritance relationships like the one above is very common, but
usually very wrong. First of all, public inheritance from
a class without virtual functions is usually a sign of bad design.
Worse is the assumption that a Square is a Rectangle in computer
science. One way to demonstrate the flaw of this design is to
introduce, set_height and set_width to Rectangle. Eg,
\begin{verbatim}
class Rectangle {
// ...
void set_width(int w) { width_ = w; }
void set_height(int h) { height_ = h; }
// ...
}
\end{verbatim}
Then you can do things like:
\begin{verbatim}
Square s(4);
s.set_width(5);
\end{verbatim}
and suddenly the invariant of Square is broken.
This demonstrates a violation of Liskov Substitution Principle.
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
struct Foo {
void f() const { std::cout << "Foo" << std::endl; }
};
struct Bar {
void f() const { std::cout << "Bar" << std::endl; };
};
template <typename T> void f(const T & t) {
t.f();
}
int main() {
f(Foo());
f(Bar());
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
Foo
Bar
\end{verbatim}
Suppose you should try to do this without a template? Then you
might want to consider creating a common class for Foo and Bar,
say Object, eg:
\begin{verbatim}
#include <iostream>
struct Object { virtual void f() const = 0; };
struct Foo : Object { void f() const { std::cout << "Foo" << std::endl; } };
struct Bar : Object { void f() const { std::cout << "Bar" << std::endl; } };
void f(const Object & o) { o.f(); }
int main() {
f(Foo());
f(Bar());
}
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
class Foo {
public:
void do_it() { p(1); func(); }
private:
virtual void func() { p(2); }
};
class Bar : public Foo {
public:
void do_it() { p(3); func(); }
private:
void func() { p(4); }
};
void run(Foo & f) {
f.do_it();
}
int main() {
Foo f;
Bar b;
run(f);
run(b);
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
scratch.cpp:5: warning: 'class Foo' has virtual functions but non-virtual destructor
scratch.cpp:12: warning: 'class Bar' has virtual functions but non-virtual destructor
1214
\end{verbatim}
If a virtual function is meant to be used only indirectly by a derived class,
it can be left private. [p738]
How to get 1234? Eg, by adding virtual to line 7.
Would it make any difference if func() on line 16 was public?
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include "Foo.hpp"
#include "Bar.hpp"
class Gaz {
Foo * f;
Bar & b;
public:
// ...
};
\end{lstlisting}
Please critizise this code.
\begin{note}
\st
\begin{tiny}
To reduce the dependency from Gaz to Bar and Foo, you might
consider, in this case, to replace the include statements
with a forward declaration. [p760]
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
// a silly implementation of a vector of integers
class IntVector {
int v[1024];
public:
int size() { return sizeof(v) / sizeof(v[0]); }
int & operator[](int i) { return v[i]; }
};
// a silly attempt to make a safe version of IntVector
class SafeIntVector : public IntVector {
public:
class OutOfRange {};
int & operator[](int i) {
if (i < 0 || i >= size())
throw OutOfRange();
return IntVector::operator[](i);
}
};
int main() {
SafeIntVector v;
v[4] = 42;
std::cout << v[4] << std::endl;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
42
\end{verbatim}
This is bad design, and is not the way to use subtyping. To
wrap around an existing type you might consider composition and
delegation, but you can also use private inheritance (also
called implementation inheritance, since it does not change
the type [p743]).
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
class Action {
public:
virtual void do_it() const = 0;
virtual ~Action() { }
};
class SayGreeting : public Action {
std::ostream & ostm_;
public:
SayGreeting() : ostm_(std::cout) {}
void do_it() const { ostm_ << "Hello" << std::endl; }
};
class SayGoodbye : public Action {
std::ostream & ostm_;
public:
SayGoodbye() : ostm_(std::cout) {}
void do_it() const { ostm_ << "Good Bye!" << std::endl; }
};
void execute(const Action & action) {
action.do_it();
}
int main() {
execute(SayGreeting());
execute(SayGoodbye());
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
Hello
Good Bye!
\end{verbatim}
\end{tiny}
\end{note}
\end{slide}
%%%%%
\renewcommand\st{§x.x, title}
\begin{slide}
\begin{lstlisting}
#include <iostream>
template<typename T> void p(T x) { std::cout << x; }
template <int min, int max> class IntRange {
int value_;
public:
class Error {};
IntRange(int value) : value_(value) {
if (value_ < min || value_ > max)
throw Error();
}
IntRange operator=(int i) {
return *this = IntRange(i);
}
operator int() {
return value_;
}
// ...
};
int main() {
IntRange<1,12> r(3);
p(1); r = 1;
p(2); r = 12;
p(3); r = 13;
p(4); r = 14;
}
\end{lstlisting}
what might happen if you try to compile, link and run this program?
\begin{note}
\st
\begin{tiny}
I get:
\begin{verbatim}
g++ -Wall scratch.cpp && ./a.out
terminate called after throwing an instance of 'IntRange<1, 12>::Error'
123
Compilation exited abnormally with code 134 at Tue Dec 4 01:23:41
\end{verbatim}
\end{tiny}
Interface classes are often used to provide C++ interfaces to non-C++
code and to insulate application code from the details of libraries. [p781]
An interface class that controls access to another class or adjusts its
interface is sometimes called a wrapper. [p781]
\end{note}
\end{slide}
%%%%%
%%
%% \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}