You are on page 1of 211

Slides

Section : Polymorphism
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism : Introduction

3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

void draw() {…} Oval

void draw() {…}


Circle

void draw() {…}

4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism : a base pointer or reference managing derived class objects

5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Passing base pointers or references to functions

6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Storing base pointers in a collection like array

7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape * ptr Circle

Rectangle

Triangle

8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with inheritance

10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

void draw() {…} Oval

void draw() {…}


Circle

void draw() {…}

11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism

Managing derived objects in memory though base pointers or references and


getting the right method called on the base pointer or reference.

12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Oval

15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Circle

16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with base class pointer

17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with base class pointer

The compiler just looks at the pointer type to decide with draw() version to
call. It sees Shape* and calls Shape::draw(). This is static binding in action!
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with base class reference

19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with base class reference

The compiler just looks at the reference type to decide with draw() version to
call. It sees Shape& and calls Shape::draw(). This is static binding in action! 20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism. Why?

21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism. Why?

22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
What we really want

23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
What we really want

24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism (dynamic binding)
with virtual functions

26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

virtual void draw() {…} Oval

virtual void draw() {…}

Circle

virtual void draw() {…}

27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Oval

29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Circle

30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Dynamic binding(polymorphism) in action!

31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Size of polymorphic objects and
slicing

33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

void draw() {…} Oval

void draw() {…}

Circle

void draw() {…}

34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Oval
virtual void draw() {…}

virtual void draw() {…}


Circle

virtual void draw() {…}

35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slicing

37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Circle

Oval Oval

Shape Shape Shape

38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Circle

Oval

Shape
= Shape

Object : shape2

Object : circle1
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape
= Shape

Object : shape2

Object : circle1
40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphic objects stored in
collections

42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Circle

Oval Oval

Shape Shape Shape

43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Oval
virtual void draw() {…}

virtual void draw() {…}


Circle

virtual void draw() {…}

44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Objects sliced off!

45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Storing in references : Won’t compile

46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Left assignability

47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Storing in pointers : Works

48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Storing in smart pointers : Works

49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Override

51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Oval
virtual void draw() {…}

virtual void draw() {…}


Circle

virtual void draw() {…}

52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Override

53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading, overriding and hiding

55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Oval
virtual void draw() {…}

virtual void draw() {…}


Circle

virtual void draw() {…}

56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual method overloads

57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual method overloads

58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One overridden overload hides all the others.

59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual overload hidden!

60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloads introduced downstream

61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloads introduced downstream

62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Inheritance and polymorphism at
different levels

64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal inheritance hierarchy

65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal Polymorphism

66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Feline Polymorphism

67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Bird Polymorphism

68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Inheritance and Polymorphism with
static members

70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Ellipse

71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Base class with static variable

72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static variable inherited

73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape and Ellipse count : similar

74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Ellipse maintains its own separate static variable

75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Ellipse maintains its own separate static variable

76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphic behavior

77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Final

79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Restrict how you override methods in derived classes
• Restrict how you can derive from a base class

80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Inheritance hierarchy

81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Dog restricts further overrides of run() downstream

82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Cat restricts further sub-classing downstream

83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Interesting fact #1 : final lone class

84
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Interesting fact #2 : Introduced useless virtual methods

85
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Can override in a final class : Makes sense!

86
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Interesting fact #3 : Introduced virtual final method

87
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

88
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Final and Override are not keywords

89
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Making them keywords would have broken code that was
written before their introduction.

90
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Final and override used as variable names : CONFUSING!

91
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Can even use them as class names

92
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Words to go by

Just because you can do something doesn’t mean you should!

93
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

94
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphic functions and access
specifiers

95
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Ellipse
virtual void draw() const;

virtual void draw() const;

96
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

public :
Ellipse
virtual void draw() const;

private :

virtual void draw() const;

97
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

private :
Ellipse
virtual void draw() const;

public :

virtual void draw() const;

98
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
99
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
100
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
When you call the virtual function through a base class pointer,
the access specifier in the base class determines whether the
function is accessible, regardless of the access specifier in the
derived class

101
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
102
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Another way to look at this

In general , when the function call is done through dynamic binding,


the access specifier of the base class applies, if the call is done through
static binding, the access specifier of the derived class applies.

103
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Guideline

As a rule of thumb, except for the base class, I mark all my other
derived overrides as private, unless the specific problem I am solving
requires otherwise.

104
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

105
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Non Polymorphic functions and
access specifiers

106
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Ellipse

107
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

public :
Ellipse
void draw() const;

private :

void draw() const;

108
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

private :
Ellipse
void draw() const;

public :

void draw() const;

109
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
When virtual functions are not used in a inheritance hierarchy, no
dynamic binding will take place, and static binding will occur,
regardless of whether methods are called through base class
pointers, references or even raw derived objects.

110
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
111
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
No overriding takes place : Just hiding methods from base

112
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding rules here!

113
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

114
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual functions with default
arguments

115
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Base

Derived

116
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Default arguments are handled at compile time
• Virtual functions are called at run time with polymorphism
• If you use default arguments with virtual functions, you might
get weird(erroneous) results with polymorphism.

117
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
118
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
119
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism in action

120
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding

121
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slicing & static binding

122
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Default arguments with virtual functions can be very confusing. They’re best avoided

123
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

124
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual Destructors

125
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal

Feline

Dog

126
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal

127
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Feline

128
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Dog

129
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Destructors called in reverse

130
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
BAD : Only Animal destructor called

131
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
If you have some dynamic memory allocated in
derived class constructors, it will just be leaked

132
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Solution : Mark your destructors virtual

133
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

134
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Dynamic_cast<>()

135
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal

Feline

Dog

136
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal* an_ptr Dog

137
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Transforming from base class pointer or reference to derived
class pointer or reference, at run time.
• Makes it possible to call non polymorphic methods on derived
objects

138
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Animal

139
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Feline

140
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Dog

141
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initially we have a base class pointer or reference

142
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Casting pointers

143
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Casting references

144
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
When casting fails

145
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Casting usually done in functions

146
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overusing down casts is a sign of bad design, if you find yourself
doing this a lot to call polymorphic functions on derived objects, may
be you should make that function polymorphic in the first place.

147
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

148
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Don’t call virtual(polymorphic) functions
from constructors & destructors

149
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Base

Derived

150
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
151
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Derived

152
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
153
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Calling a virtual function from a constructor or destructor won’t
give you polymorphic results
• The call will never go to a more derived class than the currently
executing constructor or destructor
• In other words you will get static binding results

154
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Guideline

Never call virtual functions from constructors or destructors

155
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Call virtual functions on fully constructed objects

156
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

157
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
typeid() operator

158
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Peaking on the dynamic type of a base class pointer or reference
• Works only for polymorphic types
• Returns the dynamic type if it can and the static type otherwise

159
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphic types

160
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Non polymorphic types

161
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
typeid() with fundamental types

162
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
typeid() with polymorphic types (pointer and reference)

163
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
typeid() with non polymorphic types (pointers and references)

164
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

165
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pure virtual functions and
abstract classes

166
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

Circle public : Rectangle


double perimeter()const{…}
double surface() const{…}

public :
public :
double perimeter()const{…}
double perimeter()const{…}
double surface() const{…}
double surface() const{…}

167
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pure virtual functions

168
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
169
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• If a class has at least one pure virtual function, it becomes an
abstract class
• You can’t create objects of an abstract class, if you do that , you’ll
get a hard compiler error
• Derived classes from an abstract class must explicitly override all the
pure virtual functions from the abstract parent class, if they don’t
they themselves become abstract
• Pure virtual functions don’t have an implementation in the abstract
class. They are meant to be implemented by deriving classes
• You can’t call the pure virtual functions from the constructor of the
abstract class
• The constructor of the abstract class is used by deriving class to build
up the base part of the object

170
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

171
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Abstract classes as interfaces

172
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• An abstract class with only pure virtual functions and no member variable can
be used to model what is called an interface in Object Oriented Programming.

• An interface is a specification of something that will be fully implemented in a


derived class, but the specification itself resides in the abstract class

173
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
StreamInsertable Interface

Inserting data into


std::cout streams

174
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
StreamInsertable Interface

Point

Bird
Inserting data into
std::cout streams

Dog …

175
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
StreamInsertable Interface

176
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Some translation unit

177
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Point implements the StreamInsertable interface

178
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
179
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
180
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
181
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

182
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism : Summary

183
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism : a base pointer or reference managing derived class objects

184
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Passing base pointers or references to functions

185
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Storing base pointers in a collection like array

186
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with base class pointer

The compiler just looks at the pointer type to decide with draw() version to
call. It sees Shape* and calls Shape::draw(). This is static binding in action!
187
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Static binding with base class reference

The compiler just looks at the reference type to decide with draw() version to
call. It sees Shape& and calls Shape::draw(). This is static binding in action! 188
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shape

virtual void draw() {…} Oval

virtual void draw() {…}

Circle

virtual void draw() {…}

189
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Size of polymorphic objects

190
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Circle

Oval

Shape
= Shape

Object : shape2

Object : circle1
191
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphic objects in collections : Objects sliced off!

192
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphic objects in collections : Storing in pointer Works

193
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Override

194
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading, overriding and hiding

195
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading, overriding and hiding
One overridden overload hides all the others.

196
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading, overriding and hiding
Overloads introduced downstream

197
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Polymorphism at different levels

198
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Inheritance and polymorphism with static members

199
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Final

• Restrict how you override methods in derived classes


• Restrict how you can derive from a base class

200
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Final and override are not real C++ keywords!

201
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual functions and access specifiers

Shape

public :
Ellipse
virtual void draw() const;

private :

virtual void draw() const;

202
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual functions and access specifiers

Shape

private :
Ellipse
virtual void draw() const;

public :

virtual void draw() const;

203
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual functions and default arguments

Virtual functions and default arguments

204
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual destructors

205
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Dynamic casts

206
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Virtual functions in constructors and destructors

207
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Typeid() operator

208
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pure virtual functions and abstract classes

209
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Abstract classes as interfaces

210
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

211
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya

You might also like