forked from hsf-training/cpluspluscourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedoo.tex
601 lines (567 loc) · 17.8 KB
/
advancedoo.tex
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
\subsection[advOO]{Advanced OO}
\begin{frame}[fragile]
\frametitlecpp[98]{\texttt{this} keyword}
\begin{block}{How to know an object's address?}
\begin{itemize}
\item Sometimes we need the address of the current object
\item Or we need to pass our address / a reference to a different entity
(for example to implement operators, see later)
\item All class methods can use the keyword \cppinline{this}
\begin{itemize}
\item It returns the address of the current object
\item Its type is \cppinline{T*} in the methods of a class {\ttfamily T}
\end{itemize}
\end{itemize}
\end{block}
\begin{minipage}{0.7\textwidth}
\begin{cppcode}
struct S {
int a,b;
// these two are the same:
int getB() { return b; } // 5
int getB() { return this->b; } // 5
void testAddress() {
S* addr = this; // 0x3000
}
} s{2,5};
\end{cppcode}
\end{minipage}%
\hfil%
\begin{minipage}{0.3\textwidth}
\begin{tikzpicture}
\memorystack[size x=2cm,word size=1,block size=4,nb blocks=4]
\memorypush{a = 2}
\memorypush{b = 5}
\memorystruct{1}{2}{\tiny s}
\draw[-Triangle,thick] (\stacksizex-1*\stacksizey,-1*\stacksizey) node[left] {\footnotesize \cppinline{this} pointer} -- (\stacksizex,-0.1*\stacksizey);
\end{tikzpicture}
\end{minipage}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Polymorphism}
\begin{block}{the concept}
\begin{itemize}
\item objects actually have multiple types simultaneously
\item and can be used as any of them
\end{itemize}
\end{block}
\begin{multicols}{2}
\begin{cppcode*}{}
Polygon p;
int f(Drawable & d) {...}
f(p); //ok
try {
throw p;
} catch (Shape & e) {
// will be caught
}
\end{cppcode*}
\columnbreak
\center
\begin{overprint}
\onslide<1>
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{}
\classbox[below of=Drawable]{Shape}{}
\classbox[below of=Shape]{Polygon}{}
\draw[very thick,-Triangle] (Polygon) -- (Shape);
\draw[very thick,-Triangle] (Shape) -- (Drawable);
\end{tikzpicture}
\onslide<2->
\begin{tikzpicture}
\memorystack[size x=3cm,word size=1,block size=4,nb blocks=9]
\memorypush{Drawable.a}
\memorypush{Drawable.b}
\memorypush{...}
\memorypush{Shape.a}
\memorypush{Shape.b}
\memorypush{...}
\memorypush{Polygon.nLines}
\onslide<2>{\memorystruct{1}{7}{\tiny Polygon}}
\onslide<3>{\memorystruct{1}{3}{\tiny Drawable}}
\onslide<4>{\memorystruct{1}{6}{\tiny Shape}}
\end{tikzpicture}
\end{overprint}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Inheritance privacy and polymorphism}
\begin{block}{Only public base classes are visible to outside code}
\begin{itemize}
\item private and protected bases are not
\item this may restrict usage of polymorphism
\end{itemize}
\end{block}
\begin{multicols}{2}
\begin{cppcode*}{}
Polygon p;
int f(Drawable & d) {...}
f(p); // Not ok anymore
try {
throw p;
} catch (Shape & e) {
// ok, will be caught
}
\end{cppcode*}
\columnbreak
\center
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{}
\classbox[below of=Drawable]{Shape}{}
\classbox[below of=Shape]{Polygon}{}
\draw[very thick,-Triangle] (Polygon) -- (Shape) node[midway,right] {public};
\draw[very thick,-Triangle] (Shape) -- (Drawable) node[midway,right] {private};
\end{tikzpicture}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Method overriding}
\begin{block}{the idea}
\begin{itemize}
\item a method of the parent class can be replaced in a derived class
\item but which one is called?
\end{itemize}
\end{block}
\begin{multicols}{2}
\begin{cppcode*}{}
Polygon p;
p.draw(); // ?
Shape & s = p;
s.draw(); // ?
\end{cppcode*}
\columnbreak
\center
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{
void draw();
}
\classbox[below of=Drawable]{Shape}{}
\classbox[below of=Shape]{Polygon}{
void draw();
}
\draw[very thick,-Triangle] (Polygon) -- (Shape);
\draw[very thick,-Triangle] (Shape) -- (Drawable);
\end{tikzpicture}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Virtual methods}
\begin{block}{the concept}
\begin{itemize}
\item methods can be declared \cppinline{virtual}
\item for these, the most derived object's implementation is used
(i.e.\ the dynamic type behind a pointer/reference)
\item for non-virtual methods, the static type of the variable decides
\end{itemize}
\end{block}
\begin{overprint}
\onslide<2>
\begin{multicols}{2}
\begin{cppcode*}{}
Polygon p;
p.draw(); // Polygon.draw
Shape & s = p;
s.draw(); // Drawable.draw
\end{cppcode*}
\columnbreak
\center
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{
\cppinline{void draw();}
}
\classbox[below of=Drawable]{Shape}{}
\classbox[below of=Shape]{Polygon}{
\cppinline{void draw();}
}
\draw[very thick,-Triangle] (Polygon) -- (Shape);
\draw[very thick,-Triangle] (Shape) -- (Drawable);
\end{tikzpicture}
\end{multicols}
\onslide<3>
\begin{multicols}{2}
\begin{cppcode*}{highlightlines=5}
Polygon p;
p.draw(); // Polygon.draw
Shape & s = p;
s.draw(); // Polygon.draw
\end{cppcode*}
\columnbreak
\center
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{
\cppinline{virtual void draw();}
}
\classbox[below of=Drawable]{Shape}{}
\classbox[below of=Shape]{Polygon}{
\cppinline{void draw();}
}
\draw[very thick,-Triangle] (Polygon) -- (Shape);
\draw[very thick,-Triangle] (Shape) -- (Drawable);
\end{tikzpicture}
\end{multicols}
\end{overprint}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[11]{Virtual methods - implications}
\begin{block}{Mechanics}
\begin{itemize}
\item virtual methods are dispatched at run time
\begin{itemize}
\item while non-virtual methods are bound at compile time
\end{itemize}
\item they also imply extra storage and an extra indirection
\begin{itemize}
\item practically, the object stores a pointer to the correct method
\item in a so-called ``virtual table'' (``vtable'')
\end{itemize}
\end{itemize}
\end{block}
\begin{alertblock}{Consequences}
\begin{itemize}
\item virtual methods are ``slower'' than standard ones
\item and they can rarely be inlined
\item templates are an alternative for performance-critical cases
\end{itemize}
\end{alertblock}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[11]{{\texttt override} keyword}
\begin{block}{Principle}
\begin{itemize}
\item when overriding a virtual method
\item the \cppinline|override| keyword should be used
\item the \cppinline|virtual| keyword is then optional
\end{itemize}
\end{block}
\begin{exampleblock}{Practically}
\begin{cppcode}
struct Base {
virtual void some_func(float);
};
struct Derived : Base {
void some_func(float) override;
};
\end{cppcode}
\end{exampleblock}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[11]{Why was {\texttt override} keyword introduced?}
To detect the mistake in the following code :
\begin{block}{Without {\texttt override} (\cpp98)}
\begin{cppcode}
struct Base {
virtual void some_func(float);
};
struct Derived : Base {
void some_func(double); // oops !
};
\end{cppcode}
\end{block}
\begin{itemize}
\item with \cppinline|override|, you would get a compiler error
\item if you forget \cppinline|override| when you should have it, you get a compiler warning
\end{itemize}
\end{frame}
\begin{advanced}
\begin{frame}[fragile]
\frametitlecpp[11]{{\texttt final} keyword}
\begin{block}{Idea}
\begin{itemize}
\item make sure you cannot further override a given virtual method
\item by declaring it final
\end{itemize}
\end{block}
\begin{exampleblock}{Practically}
\begin{cppcode}
struct Base {
virtual void some_func(float);
};
struct Intermediate : Base {
void some_func(float) final;
};
struct Derived : Intermediate {
void some_func(float) override; // error
};
\end{cppcode}
\end{exampleblock}
\end{frame}
\end{advanced}
\begin{frame}[fragile]
\frametitlecpp[11]{Pure Virtual methods}
\begin{block}{Concept}
\begin{itemize}
\item unimplemented methods that must be overridden
\item marked by \cppinline{= 0} in the declaration
\item makes their class abstract
\item only non-abstract classes can be instantiated
\end{itemize}
\end{block}
\pause
\begin{multicols}{2}
\begin{cppcode*}{}
// Error : abstract class
Shape s;
// ok, draw has been implemented
Polygon p;
// Shape type still usable
Shape & s = p;
s.draw();
\end{cppcode*}
\columnbreak
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{
\cppinline{virtual void draw() = 0;}
}
\classbox[below of=Drawable]{Shape}{}
\classbox[below of=Shape]{Polygon}{
\cppinline{void draw() override;}
}
\draw[very thick,-Triangle] (Polygon) -- (Shape);
\draw[very thick,-Triangle] (Shape) -- (Drawable);
\end{tikzpicture}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Polymorphism and destruction}
\begin{block}{Owning base pointers}
We sometimes need to maintain owning pointers to base classes:
\end{block}
\begin{overprint}
\onslide<1>
\begin{cppcode}
struct Drawable {
virtual void draw() = 0;
};
Drawable* getImpl();
Drawable* p = getImpl();
p->draw();
delete p;
\end{cppcode}
\onslide<2>
\begin{cppcode}
struct Drawable {
virtual void draw() = 0;
};
std::unique_ptr<Drawable> getImpl(); // better API
auto p = getImpl();
p->draw();
\end{cppcode}
\end{overprint}
\begin{block}{}
\begin{itemize}
\item What happens when \cppinline{p} is deleted?
\item What if a class deriving from \cppinline{Drawable} has a destructor?
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[11]{Polymorphism and destruction}
\begin{block}{Virtual destructors}
\begin{itemize}
\item We can mark a destructor as \cppinline{virtual}
\item This selects the right destructor based on the runtime type
\end{itemize}
\end{block}
\begin{cppcode}
struct Drawable {
virtual ~Drawable() = default;
virtual void draw() = 0;
};
Drawable* p = getImpl(); // returns derived obj.
p->draw();
delete p; // dynamic dispatch to right destructor
\end{cppcode}
\begin{goodpractice}{Virtual destructors}
If you expect users to inherit from your class and override methods (i.e.\ use your class polymorphically), declare its destructor \cppinline{virtual}
\end{goodpractice}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[11]{Pure Abstract Class aka Interface}
\begin{block}{Definition of pure abstract class}
\begin{itemize}
\item a class that has
\begin{itemize}
\item no data members
\item all its methods pure virtual
\item a \cppinline{virtual} destructor
\end{itemize}
\item the equivalent of an Interface in Java
\end{itemize}
\end{block}
\begin{multicols}{2}
\begin{cppcode*}{}
struct Drawable {
virtual ~Drawable()
= default;
virtual void draw() = 0;
}
\end{cppcode*}
\columnbreak
\center
\begin{tikzpicture}[node distance=1.5cm]
\classbox{Drawable}{
virtual void draw() = 0;
}
\end{tikzpicture}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Overriding overloaded methods}
\begin{block}{Concept}
\begin{itemize}
\item overriding an overloaded method will hide the others
\item unless you inherit them using \cppinline{using}
\end{itemize}
\end{block}
\begin{cppcode*}{}
struct BaseClass {
virtual int foo(std::string);
virtual int foo(int);
};
struct DerivedClass : BaseClass {
using BaseClass::foo;
int foo(std::string) override;
};
DerivedClass dc;
dc.foo(4); // error if no using
\end{cppcode*}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Polymorphism}
\begin{exercise}{Polymorphism}
\begin{itemize}
\item go to \texttt{exercises/polymorphism}
\item look at the code
\item open trypoly.cpp
\item create a Pentagon, call its perimeter method
\item create a Hexagon, call its perimeter method
\item create a Hexagon, call its parent's perimeter method
\item retry with virtual methods
\end{itemize}
\end{exercise}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Multiple Inheritance}
\begin{block}{Concept}
\begin{itemize}
\item one class can inherit from multiple parents
\end{itemize}
\end{block}
\begin{multicols}{2}
\begin{tikzpicture}[]
\classbox[]{Polygon}{}
\classbox[below of=Polygon,node distance=1.5cm]{Rectangle}{}
\classbox[right of=Rectangle,node distance=3cm]{Text}{}
\classbox[below right of=Rectangle,node distance=2cm]{TextBox}{}
\draw[very thick,Triangle-] (Polygon) -- (Rectangle);
\draw[very thick,Triangle-] (Rectangle) -- (TextBox);
\draw[very thick,Triangle-] (Text) -- (TextBox);
\end{tikzpicture}
\columnbreak
\vspace{2cm}
\begin{cppcode*}{}
class TextBox :
public Rectangle, Text {
// inherits from both
// publicly from Rectangle
// privately from Text
}
\end{cppcode*}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{The diamond shape}
\begin{block}{Definition}
\begin{itemize}
\item situation when one class inherits several times from a given grand parent
\end{itemize}
\end{block}
\begin{alertblock}{Problem}
\begin{itemize}
\item are the members of the grand parent replicated?
\end{itemize}
\end{alertblock}
\vfill
\hspace{2.5cm}
\begin{tikzpicture}[]
\classbox[]{Drawable}{}
\classbox[below left of=Drawable,node distance=2cm]{Rectangle}{}
\classbox[right of=Rectangle,node distance=3cm]{Text}{}
\classbox[below right of=Rectangle,node distance=2cm]{TextBox}{}
\draw[very thick,Triangle-] (Drawable) -- (Rectangle);
\draw[very thick,Triangle-] (Drawable) -- (Text);
\draw[very thick,Triangle-] (Rectangle) -- (TextBox);
\draw[very thick,Triangle-] (Text) -- (TextBox);
\end{tikzpicture}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Virtual inheritance}
\begin{block}{Solution}
\begin{itemize}
\item inheritance can be \cppinline{virtual} or not
\begin{itemize}
\item \cppinline{virtual} inheritance will ``share'' parents
\item standard inheritance will replicate them
\end{itemize}
\item most derived class will call the virtual base class's constructor
\end{itemize}
\begin{cppcode}
class Text : public virtual Drawable {...};
class Rectangle : public virtual Drawable {...};
\end{cppcode}
\end{block}
\begin{multicols}{2}
\begin{tikzpicture}[]
\classbox{Drawable}{}
\classbox[below left of=Drawable,node distance=2cm]{Rectangle}{}
\classbox[right of=Rectangle,node distance=3cm]{Text}{}
\classbox[below right of=Rectangle,node distance=2cm]{TextBox}{}
\draw[very thick,Triangle-] (Drawable) -- node[below,pos=0.35,sloped] {\scriptsize virtual} (Rectangle);
\draw[very thick,Triangle-] (Drawable) -- node[below,pos=0.45,sloped] {\scriptsize virtual} (Text);
\draw[very thick,Triangle-] (Rectangle) -- (TextBox);
\draw[very thick,Triangle-] (Text) -- (TextBox);
\end{tikzpicture}
\columnbreak
\begin{tikzpicture}[]
\classbox[]{Drawable1}{}
\classbox[below of=Drawable1,node distance=1.5cm]{Rectangle}{}
\draw[very thick,Triangle-] (Drawable1) -- (Rectangle);
\classbox[right of=Drawable1,node distance=3cm]{Drawable2}{}
\classbox[below of=Drawable2,node distance=1.5cm]{Text}{}
\draw[very thick,Triangle-] (Drawable2) -- (Text);
\classbox[below right of=Rectangle,node distance=2cm]{TextBox}{}
\draw[very thick,Triangle-] (Rectangle) -- (TextBox);
\draw[very thick,Triangle-] (Text) -- (TextBox);
\end{tikzpicture}
\end{multicols}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Multiple inheritance advice}
\begin{goodpractice}{Avoid multiple inheritance}
\begin{itemize}
\item Except for inheriting from interfaces (=no data members)
\item And for rare special cases
\end{itemize}
\hspace*{0.05\textwidth}\begin{minipage}{0.9\textwidth}
\begin{alertblock}{Absolutely avoid diamond-shape inheritance}
\begin{itemize}
\item This is a sign that your architecture is not correct
\item In case you are tempted, think twice and change your mind
\end{itemize}
\end{alertblock}
\end{minipage}
\end{goodpractice}
\end{frame}
\begin{frame}[fragile]
\frametitlecpp[98]{Virtual inheritance}
\begin{exerciseWithShortcut}{Virtual inheritance}{Virtual OO}
\begin{itemize}
\item go to \texttt{exercisescode/virtual\_inheritance}
\item look at the code
\item open trymultiherit.cpp
\item create a TextBox and call draw
\item Fix the code to call both draws by using types
\item retry with virtual inheritance
\end{itemize}
\end{exerciseWithShortcut}
\end{frame}