Section_Operator_Overloading_Slides
Section_Operator_Overloading_Slides
Section : Operator
Overloading
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
Operator overloading
3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator+ (as member)
7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Return type operator [x] (parameters)
{
//Body
}
9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
p1 + p2 p1.operator+(p2)
12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator+ (as NON member)
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Return type operator [x] (parameters)
{
//Body
}
16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
p1 + p2 operator+(p1,p2)
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading the subscript
operator for reading
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
int value = scores[5]
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• The subscript operator is a binary operator
• It is one of the operators that MUST be set up as a member function
24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
point1[0] point1.operator[](0)
25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading the subscript
operator [Read Write]
27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Subscript operator for collection
types
31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Stream insertion operator
35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator << as member function
38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
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
Stream extraction operator
42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
stream
network
stream
45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Arithmetic Operators
47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Just because you can overload an operator doesn’t mean you should
48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Point : Arithmetic Operators
49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compound operators - Reusing
Operators
52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Point : Arithmetic Operators
53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Custom Type Conversions
56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Set up the transformations you want to support
from your type to other types
57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Conversion :
• From Number to double
• From Number to Point
59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Setting up type conversions
60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Using type conversions
61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Ambiguity on some compilers
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
Implicit Conversions with
Overloaded binary operators
64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
When a binary operator is implemented as a member
function, the left operand is never implicitly converted
65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Implicit conversions don’t work for the left operand
66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading the prefix ++
operator
69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Overloading the prefix ++
operator as non member
73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unary postfix increment
operator
77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Members
79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Non members
80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Prefix/postfix decrement
operator
82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
84
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy assignment operator
85
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy assignment operator
p1 p2
m_x = m_x
m_y m_y
Int *p_data Int * p_data
86
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
If you have no custom copy assignment operator in place, the compiler is
going to generate one for you
The compiler generated one is going to do member wise copy
87
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
88
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator chaining
89
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Watch out!
90
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Self assignment
91
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Why check for self assignment
92
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Why check for self assignment
p1 p2
=
Int* data_p Int* data_p
• Release memory in p1
• Allocate new dynamic memory for data_p
• Copy in data from p2
93
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Why check for self assignment
p1 p1
=
Int* data_p Int* data_p
• Release memory in p1
• Allocate new dynamic memory for data_p
• Copy in data from p1
94
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Self assignment with copy constructors
95
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
96
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy assignment operator for
other types
97
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
98
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
99
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
100
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Custom Type conversions :
A Recap
101
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
102
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Type Conversion Operator : Number -> Point
• Constructor taking Number in : Number -> Point
• Copy assignment operator for Number : Number -> Point
103
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Type conversion operator
104
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constructor taking in a Number
105
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy assignment operator for Number
106
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
107
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Functors
108
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Objects of a class that overloads the () operator
109
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
110
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
111
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator overloading :
Summary
112
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
113
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
114
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator+ as a member
115
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Operator+ as a non member
116
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Subscript operator
117
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Stream insertion operator
118
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Stream extraction operator
119
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compound operators and reusing others
120
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Custom Type Conversions
121
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Implicit conversions with member binary operators
122
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Unary Increment operator
123
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy assignment operator
124
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy assignment for other types
125
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Functors
126
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Type conversions
• Constructors
• Custom Type Conversion Operators
• Copy assignment operators for different types
127
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
128
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya