22
Chapter 14 Operator Overloading: What does + mean? Consider the code below. Note the multiple uses of “+”. #include <iostream> #include <string> using namespace std; int main() { int a,b=5,c=8; string x, y = “Five”, z = “Six”; a = b + c; x = y + z; cout << a << “ “ << x << endl; }

Chapter 14 Operator Overloading:

  • Upload
    nguyet

  • View
    54

  • Download
    1

Embed Size (px)

DESCRIPTION

Chapter 14 Operator Overloading:. What does + mean? Consider the code below. Note the multiple uses of “+”. #include < iostream > #include using namespace std; int main() { int a,b =5,c=8; string x, y = “Five”, z = “Six”; a = b + c; x = y + z; - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 14 Operator Overloading:

Chapter 14 Operator Overloading:

What does + mean?Consider the code below. Note the multiple uses of “+”.

#include <iostream>#include <string>using namespace std;int main(){

int a,b=5,c=8;string x, y = “Five”, z = “Six”;

a = b + c;x = y + z;cout << a << “ “ << x << endl;

}

Page 2: Chapter 14 Operator Overloading:

The operator “+” is an overloaded operator.

That is, it has different meanings depending on how it is used (its context).

Programmers can give new meaning to existing operators.

This is operator overloading.

Page 3: Chapter 14 Operator Overloading:

Example: if x and y are objects, what does x+y mean?What if x and y are fractions?

x = a/b and y = c/d; Then x + y= (ad+bc)/bd?

Page 4: Chapter 14 Operator Overloading:

P. 547 shows overloadable operators.Fraction case study Section 14.2. Also on program demo. Step through various constructorsDiscuss the normalize method

Page 5: Chapter 14 Operator Overloading:

Overloaded Operators: implement as member functions or non-member functions (aka helper functions).Note that there are some of each in demo07.Which is best?

Page 6: Chapter 14 Operator Overloading:

Fraction class: code shows ‘+’ operator as both member and helper, though if one is used the other commented out.If a member function then x+y is the same as x.operator+(y)

Page 7: Chapter 14 Operator Overloading:

Thus, x+2 is OK because 2 is automatically promoted to a fraction using the default constructorbut 2+x is not OK since it is interpreted as 2.operator+(x)Similar to example on p. 549.If a helper function, either is OK.See code.

Page 8: Chapter 14 Operator Overloading:

Similar comments for other arithmetic operators ‘-’, ‘*’, and ‘/’.Note the productivity hint on page 559.Note the productivity hint on page 560.

Page 9: Chapter 14 Operator Overloading:

Note the advanced topic on page 561. This is important because it’s a basic design issue – when to use member functions or helper functions. Same issue as with ‘+’ … symmetry is important with the ‘==‘ (and other comparison) operators

Page 10: Chapter 14 Operator Overloading:

Overloading the ++ operators – page 564.

Note that one returns a reference and the other returns a copy. Note the prose on page 565

Note the quality tip on page 565.Skip the section on overloaded operators and iterators. It requires things we’ve not covered yet.

Page 11: Chapter 14 Operator Overloading:

Overloading the assignment operator p. 568.Not done for the Fraction class; C++ provides its own overaloded operator – THOUGH THIS CAN GET YOU IN TROUBLE!!! (more later in the semester).See the productivity hint on page 568.

Page 12: Chapter 14 Operator Overloading:

Overloading conversion operators. Remove comments around the double operator.Note the error. What is it telling you?

Page 13: Chapter 14 Operator Overloading:

Adds ambiguity to the code. C++ does not know what to do.For example, the overloaded operator a = a+2 does not work if the conversion operator double() is included in the class definition.Compiler error that detects there is an ambiguity between the two.

Page 14: Chapter 14 Operator Overloading:

Reason:Compiler sees “b=a+2” and must interpret “+”.It knows that 2 can be converted to a fraction and could use the + that adds two fractions.It also knows that “a” can be converted to a double and can use the + that adds a double and an int.

Page 15: Chapter 14 Operator Overloading:

Thus the message “error C2666: ‘operator`+’’ : 2 overloads have similar conversions”.More at [http://support.microsoft.com/default.aspx?scid=kb;en-us;106392].

Page 16: Chapter 14 Operator Overloading:

Removing the double() conversion operator will fix this.Can also fix by adding additional overloaded operators. For example:

Fraction operator+(int left, const Fraction& right){ Fraction temp(left); return temp + right;}

Page 17: Chapter 14 Operator Overloading:

This would allow an expression of the form a = 2 + a.

However, would need yet another overloaded operator to allow a + 2.

Note the tips, errors, and advanced topics on pages 570-2.Putting explicit before the constructor Fraction (int t) prevents the automatic conversion from an int to a Fraction in a+2.

Page 18: Chapter 14 Operator Overloading:

Overloading the subscript operator: See the SafeArray class on p. 573 AND the code snippet from the notes.The [] operator hides the details of how an element is accessed.This is WHY we do encapsulation.

Page 19: Chapter 14 Operator Overloading:

Note that the overloaded operator is written as a member function.Note the error if I try to write x[2] = 99 in the main code.

Must write another overloaded operator[] (non-constant) that returns int& (i.e. an lvalue).

Functions that overload operators should return reference types if the operator will be used as an lvalue.

Page 20: Chapter 14 Operator Overloading:

See example on p. 574 on overloading the function call operator. Again, must be written as a member function.

Page 21: Chapter 14 Operator Overloading:

Program demo08 has overloaded operators << and >>. At this point it’s mostly just a matter of replacing read() and write() methods with >> and <<. However, later when templates are discussed you’ll see that you can define a single << or >> operator to work with ANY array. In the overloaded operator code you’ll apply << or >> to each array element which could be any type or class. This allows a single method to be used with a large range of types. Very generic.

Page 22: Chapter 14 Operator Overloading:

Demo08 does show how this can be done. Put a break point in the Manager destructor and show how writing BOTH the account and customer lists is done via the SAME overloaded << operator.