220 likes | 314 Views
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;
E N D
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() { inta,b=5,c=8; string x, y = “Five”, z = “Six”; a = b + c; x = y + z; cout << a << “ “ << x << endl; }
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.
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?
P. 547 shows overloadable operators. • Fraction case study Section 14.2. Also on program demo. • Step through various constructors • Discuss the normalize method
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?
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)
Thus, x+2 is OK because 2 is automatically promoted to a fraction using the default constructor • but 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.
Similar comments for other arithmetic operators ‘-’, ‘*’, and ‘/’. • Note the productivity hint on page 559. • Note the productivity hint on page 560.
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
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.
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.
Overloading conversion operators. • Remove comments around the double operator. • Note the error. What is it telling you?
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.
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.
Thus the message “error C2666: ‘operator`+’’ : 2 overloads have similar conversions”. • More at [http://support.microsoft.com/default.aspx?scid=kb;en-us;106392].
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; }
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.
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.
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.
See example on p. 574 on overloading the function call operator. • Again, must be written as a member function.
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.
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.