1 / 22

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;

nguyet
Télécharger la présentation

Chapter 14 Operator Overloading:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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; }

  2. 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.

  3. 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?

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

  5. 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?

  6. 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)

  7. 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.

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

  9. 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

  10. 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.

  11. 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.

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

  13. 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.

  14. 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.

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

  16. 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; }

  17. 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.

  18. 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.

  19. 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.

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

  21. 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.

  22. 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.

More Related