3.operator Overloading
3.operator Overloading
3.operator Overloading
Lecture 4 Operator
Overloading (C++)
Lecture adapted from
C++ How to Program (9th Edition)
By Paul Deitel and Harvey Deitel
Prentice Hall, Inc. All rights reserved.
imranihsan.com
+ 4.1 Introduction
Examples
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
+ 4.2
Types
Overloading operators
imranihsan.com
+ 4.2
OOP 04 - Operator
Overloading
object2 = object1.add(object2);
object2 = object2 + object1;
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
Cannot change
OOP 04 - Operator
Overloading
imranihsan.com
&
<
>
+=
-=
*=
/=
%=
^=
&=
|=
<<
>>
>>=
<<=
==
!=
<=
>=
&&
||
++
--
->*
->
[]
()
new
delete
new[]
delete[]
imranihsan.com
.*
::
?:
sizeof
OOP 04 - Operator
Overloading
5/14/2014
+ 4.4
Operator functions
Member functions
Use this keyword to implicitly get argument
Gets left operand for binary operators (like +)
Leftmost object must be of same class as operator
Non member functions
Need parameters for both operands
Can have object of different class than operator
Must be a friend to access private or protected data
Called when
Left operand of binary operator of same class
Single operand of unitary operator of same class
imranihsan.com
OOP 04 - Operator
Overloading
+ 4.4
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
+ 4.4
Commutative operators
imranihsan.com
OOP 04 - Operator
Overloading
+ 4.5
Example program
Class PhoneNumber
Holds a telephone number
Print out formatted number automatically
(123) 456-7890
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
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
std::cout;
std::cin;
std::endl;
std::ostream;
std::istream;
#include <iomanip>
using std::setw;
OOP 04 - Operator
Overloading
imranihsan.com
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
OOP 04 - Operator
Overloading
5/14/2014
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
imranihsan.com
+ 4.6
OOP 04 - Operator
Overloading
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
+ 4.6
Upcoming example
imranihsan.com
+ 4.7
OOP 04 - Operator
Overloading
Upcoming example
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
+ 4.7
Upcoming example
imranihsan.com
+ 4.8
Arrays in C++
OOP 04 - Operator
Overloading
No range checking
Cannot be compared meaningfully with ==
No array assignment (array names const pointers)
Cannot input/output entire arrays at once
One element at a time
Range checking
Array assignment
Arrays that know their size
Outputting/inputting entire arrays with << and >>
Array comparisons with == and !=
imranihsan.com
OOP 04 - Operator
Overloading
5/14/2014
+ 4.8
Copy constructor
OOP 04 - Operator
Overloading
imranihsan.com
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
//
//
//
//
default constructor
copy constructor
destructor
return size
// assignment operator
const Array &operator=( const Array & );
// equality operator
bool operator==( const Array & ) const;
imranihsan.com
OOP 04 - Operator
Overloading
10
5/14/2014
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
OOP 04 - Operator
Overloading
imranihsan.com
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
#include <cstdlib>
#include "array1.h"
imranihsan.com
OOP 04 - Operator
Overloading
11
5/14/2014
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
OOP 04 - Operator
Overloading
imranihsan.com
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
imranihsan.com
OOP 04 - Operator
Overloading
12
5/14/2014
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
return *this;
OOP 04 - Operator
Overloading
imranihsan.com
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
} // end if
return ptr[ subscript ]; // reference return
} // end function operator[]
imranihsan.com
OOP 04 - Operator
Overloading
13
5/14/2014
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
} // end if
return ptr[ subscript ]; // const reference return
} // end function operator[]
// overloaded input operator for class Array;
// inputs values for entire array
istream &operator>>( istream &input, Array &a )
{
for ( int i = 0; i < a.size; i++ )
input >> a.ptr[ i ];
return input;
} // end function
OOP 04 - Operator
Overloading
imranihsan.com
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
imranihsan.com
OOP 04 - Operator
Overloading
14
5/14/2014
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
// seven-element Array
// 10-element Array by default
imranihsan.com
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
OOP 04 - Operator
Overloading
imranihsan.com
OOP 04 - Operator
Overloading
15
5/14/2014
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
OOP 04 - Operator
Overloading
imranihsan.com
+
Size of array integers1 is 7
Array after initialization:
0
0
0
0
Size of array integers2 is 10
Array after initialization:
0
0
0
0
0
0
0
0
0
0
0
0
Input 17 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
After input, the arrays contain:
integers1:
1
2
5
6
integers2:
8
9
12
13
imranihsan.com
3
7
10
14
11
15
OOP 04 - Operator
Overloading
16
5/14/2014
3
7
10
14
11
15
10
14
11
15
OOP 04 - Operator
Overloading
imranihsan.com
10
14
11
15
imranihsan.com
OOP 04 - Operator
Overloading
17