Chapter 8 Operator Overloading Operators You Can Overload













![More on Overloading w You can overload other things like the [] symbols, and More on Overloading w You can overload other things like the [] symbols, and](https://slidetodoc.com/presentation_image_h2/8206090a543cc7b3940b62c24ea1e4e5/image-14.jpg)
- Slides: 14

Chapter 8 Operator Overloading

Operators You Can Overload w You can overload any of the following operators: w +, -, *, /, %, ^, &, |, ~, !, =, <, >, +=, -=, *=, /=, %=, ^=, &=, |=, <<, >>, <<=, >>=, ==, !=, <=, >=, &&, ||, ++, -- , ->*, ->, ( ), [ ], new, delete, new[], delete[]

Postfix Notation w So far we have only shown the increment operator used only in its prefix form. w ++c 1 w But what about postfix, where the variable is incremented after its value is used in the expression? w c 1++;

Postfix Notation w To make both versions of the increment operator work, we defined two overloaded ++ operators. w Let’s look at an example of this in “postfix. cpp”.

Arithmetic Operators w A past example that we saw was to add two Distance objects together. w In this program we saw the statement: dist 3. add_dist(dist 1, dist 2); w By overloading the + operator we can reduce this strange looking expression to: d 3 = dist 1 + dist 2;

Arithmetic Operators w Let’s look at a new version of this distance program, “englplus. cpp”.

Arithmetic Operators w Similar functions could be created to overload other operators in the Distance class, so you could subtract, multiply, and divide objects of this class in an easier to read format.

Comparison Operators w Let’s look at an example where we will overload the less than operator ( < ) in the Distance class so that we can compare two distances. w We can see an example in “engless. cpp”.

Comparing Strings w Now we will look at another example of overloading an operator, this time the equal to (==) operator. w We’ll use this == operator to compare two of our homemade String objects, returning true if they’re the same and false if they’re different. w Let’s look at “strequal. cpp”.

Arithmetic Assignment Operators w It might not be as obvious how we would overload the += operator. w Remember that this operator combines assignment and addition into one step. w Let’s look at an example of this in our Distance class, “englpleq. cpp”.

Arithmetic Assignment Operators w In the operator +=() function in the last example, the object that takes on the value of the sum is the object of which the function is a member. w So it is feet and inches that are given values, not temporary variables used only to return an object.

Arithmetic Assignment Operators w The operator+=() function has no return value; it returns type void. w A return value is not necessary with arithmetic assignment operators such as +=, because the result of the assignment operator is not assigned to anything.

Arithmetic Assignment Operators w The operator is used alone, in expressions like the one in the program: dist 1 += dist 2; w If you wanted to use this operator in more complex expressions, like: dist 3 = dist 1 += dist 2; w Then you would need to provide a return value. You can do this by ending the operator+=() function with a statement like w return Distance(feet, inches); w This is a nameless object that is initialized to the same values as this object and then returned.
![More on Overloading w You can overload other things like the symbols and More on Overloading w You can overload other things like the [] symbols, and](https://slidetodoc.com/presentation_image_h2/8206090a543cc7b3940b62c24ea1e4e5/image-14.jpg)
More on Overloading w You can overload other things like the [] symbols, and you can convert types between user defined objects. w All of these subjects can be found in our book for future reference.