0
Upcoming SlideShare
×

16,187

Published on

6 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• thanks, the document will help me very much...

Are you sure you want to  Yes  No
• thank u soo much it reay help me

Are you sure you want to  Yes  No
Views
Total Views
16,187
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
740
2
Likes
6
Embeds 0
No embeds

No notes for slide

2. 2. Understanding the Benefits of Overloading <ul><li>Having more than one function with the same name is beneficial because you can use one easy-to-understand function name without paying attention to the data types involved </li></ul><ul><li>Polymorphism allows the same operation to be carried out differently, depending on the object </li></ul><ul><li>Some reserve the term polymorphism (or pure polymorphism) for situations in which one function body is used with a variety of arguments </li></ul>
3. 3. Using the + Operator Polymorphically <ul><li>Separate actions can result from what seems to be the same operation or command </li></ul><ul><li>The + operator has a variety of meanings, which include: </li></ul><ul><ul><li>Alone before a value (called unary form), + indicates a positive values, as in the expression +7 </li></ul></ul><ul><ul><li>Between two integers (called binary form), + indicates integer addition, as in the expression 5+ 9 </li></ul></ul><ul><ul><li>Between two floating-point numbers (also called binary form), + indicates floating-point addition, as in the expression 6.4 + 2.1 </li></ul></ul>
4. 4. Overloading Operators— The Rules <ul><li>Operator overloading is the process by which you apply operators to your own abstract data types </li></ul><ul><li>The +, -, *, and / symbols make it easy to work with built-in data types such as int and double </li></ul><ul><li>Classes, however, contain a variety of data members </li></ul><ul><li>As a result, if you want the compiler to perform arithmetic with two class objects, you must tell the compiler what you mean </li></ul><ul><li>Good programming style dictates that you endow the operator with a reasonable meaning </li></ul>
5. 5. Overloading Operators— The Rules <ul><li>You overload an operator by making it a function; subsequently, you can use it just like any other function </li></ul><ul><li>C++ operators are classified as unary or binary, depending on whether they take one or two arguments, respectively </li></ul>
6. 6. Binary Operators that Can Be Overloaded
7. 7. Overloading Operators— The Rules <ul><li>Associativity refers to the order in which actions within an expression are carried out </li></ul><ul><li>You cannot change associativity when you overload operators </li></ul><ul><li>You also cannot change the normal precedence of any operator </li></ul>
9. 9. Overloading Math Operators <ul><li>When you code an expression such as 4 + 7, C++ understands that you intend to carry out binary integer addition because of the context of the + symbol </li></ul><ul><li>When you code an expression such as regularSal + bonus , if C++ can recognize regularSal and bonus as declared double variables, then floating-point addition takes place </li></ul><ul><li>The name of the operator function that overloads the + symbol is operator+() </li></ul>
11. 11. Overloading Math Operators <ul><li>The operator+() function in Figure 8-1 can work like any other member function </li></ul><ul><li>When you examine the code for the addTwo() and operator+() functions in Figure 8-1, you see that the only difference is the function name </li></ul><ul><li>Instead of the awkward sum = clerk.operator+(driver);, the operator+() function allows you to leave off the word operator in the function name and add either of the following statements: </li></ul><ul><ul><li>sum = clerk + driver; </li></ul></ul><ul><ul><li>sum = driver + clerk; </li></ul></ul>
13. 13. Paying Attention to the Order of the Operands <ul><li>You can choose to overload any of the arithmetic operators for any classes you develop </li></ul><ul><li>Then you can use the corresponding operator symbol in a natural way with class objects </li></ul>
14. 14. Overloading an Operator to Work with a Class Object and a Primitive Type <ul><li>When you add two objects using the + operator, the objects do not have to be the same type </li></ul><ul><li>You can add an integer and a double with an expression such as 5 + 7.84 </li></ul>Ex8-3
15. 15. Overloading an Operator to Work with a Class Object and a Primitive Type <ul><li>You cannot overload operators that work with C++’s built-in data types </li></ul><ul><li>You cannot overload the + that works with two doubles to make it do anything but add two doubles </li></ul><ul><li>Similarly, you can’t overload operators whose first operand is an object that is a built-in type, even if the second operand is a class object </li></ul>
16. 16. Using Multiple Operations in a Statement <ul><li>Most modern programming languages allow several operators to be used in the same statement </li></ul><ul><li>If you want to sum three values in an older programming language such as assembler or RPG, you first must add two values, producing a temporary total </li></ul><ul><li>Then, in a separate statement, you add the third value to that total </li></ul>
17. 17. The Sale Class Ex8-4
18. 18. Using Multiple Operations in a Statement <ul><li>Because the associativity of addition occurs from left to right, the attempt to execute the addition highlighted in Figure 8-9 follows this sequence: </li></ul><ul><ul><li>1. The left-most + operator is encountered, and C++ recognizes a Sale on each side of the + symbol. The overloaded operator+() function is called, and saleAmounts for a Shirt and a Tie are added. A double is returned </li></ul></ul><ul><ul><li>2. The next + operator is encountered. A Sale object is found as the operand to the right of the +, but a double value is used as the operand to the left </li></ul></ul>
19. 19. Program that Adds Three Sale Objects Ex8-4
20. 20. Using Multiple Operations in a Statement <ul><li>When the Sale class operator+() function does not return a double, but instead returns an object of Sale type (as shown in Figure 8-8), the multiple addition works correctly </li></ul><ul><li>The sequence of events now occurs as follows: </li></ul><ul><ul><li>1. The left-most + operator is encountered, and C++ recognizes a Sale object on each side of the + symbol. The overloaded operator+() function is called, and saleAmounts for a Shirt and a Tie are added </li></ul></ul><ul><ul><li>2. The next + operator is encountered. A Sale object now is found on each side of the +—the temporary object returned by the first addition, and the pants object </li></ul></ul><ul><ul><li>3. The temporary object is assigned to the total Sale object </li></ul></ul>
21. 21. Using Multiple Operations in a Statement <ul><li>The results of the execution of the program in Figure 8-9 are shown in Figure 8-10 </li></ul><ul><li>C++ forces you to use the built-in precedence rules for your class operators </li></ul><ul><li>If you want to be able to add either a double or a Sale object to a Sale object, then simply write both versions of the overloaded operator for the class </li></ul>
22. 22. Overloading Output <ul><li>The << operator also is overloaded by C++ </li></ul><ul><li>It is both a bitwise left-shift operator and an output operator; it is called the insertion operator when used for output </li></ul><ul><li>The << operator acts as an output operator only when cout (or another output stream object) appears on the left side </li></ul><ul><li>When you use cout in a program, you must include #include<iostream.h> </li></ul><ul><li>The preceding function, called operator<<(), returns a reference to ostream </li></ul>
23. 23. Overloading Output <ul><li>It accepts two arguments: a reference to ostream (locally named out in this example) and an integer (locally named i n in this example) </li></ul><ul><li>C++ overloads the << operator to work with the built-in data types; you also may overload the << operator to work with your own classes </li></ul><ul><li>To overload << operator so it can work with a Sale object, you must add the overloaded operator <<() function to the Sale class </li></ul>
24. 24. Overloading Output <ul><li>The operator <<() function is a friend to the class of the object it wants to print out, e.g. Sale here. </li></ul>
25. 25. Overloading Input <ul><li>If the << operator can be overloaded for output, it makes sense that the >> operator also can be overloaded for input </li></ul><ul><li>The advantage of overloading operators such as >> is that the resulting programs look cleaner and are easier to read </li></ul><ul><li>You can create an extraction operator, or operator>>() function, that uses istream (which is defined in iostream.h, along with ostream) by using a prototype as follows: </li></ul><ul><ul><li>friend istream& operator>>(istream &in, Sale &Sale); </li></ul></ul>
26. 26. Overloaded Operator>>() Function for the Sale Class
28. 28. Overloading ++ and - - <ul><li>With C++, you use ++ to increment variables, and - - to decrement variables </li></ul><ul><li>When a prefix operator such as ++ is used in an expression, the mathematical operation takes place before the expression is evaluated </li></ul><ul><li>When the postfix operator is used, the expression is evaluated before the mathematical operation takes place </li></ul><ul><li>Within the operator ++() function in the Inventory class, you can write the statement that increases numSold in several different ways </li></ul>
29. 29. Using the Prefix and Postfix ++ Operators with an Integer
30. 30. The Inventory Class
31. 31. Overloading ++ and - - <ul><li>The statements numSold++;, numSold = numSold +1;, and numSold += 1; all would work </li></ul>8 Ex8-8
32. 32. Using Postfix Increment and Decrement Operators <ul><li>A problem arises if you want to use a postfix ++ operator as well as a prefix ++ operator with a class </li></ul><ul><li>When you overload any C++ function, you must supply different argument lists; for the postfix ++ operator, you use an integer argument </li></ul><ul><li>The Inventory class postfix operator ++() function prototype is: </li></ul><ul><ul><li>Inventory& operator++(int); </li></ul></ul>Ex8-8
33. 33. Overloading the = = Operator <ul><li>Writing an operator = =() function should be an easy task </li></ul><ul><li>You simply decide what will constitute equality in class members </li></ul><ul><li>When you create your own classes, you choose whether equivalency means that every data field must be equivalent, or only specific data members </li></ul><ul><li>The operator = =() function may return either an integer or a boolean variable representing true or false </li></ul>8
34. 34. Overloading the = = Operator <ul><li>A variable of type bool can hold one of two values: true or false </li></ul><ul><li>Some older C++ compilers do not support the bool type; with those compilers you would use the first version of operator = =() that returns an integer </li></ul>EX8-9
35. 35. Overloading the = Operator <ul><li>The = operator can be overloaded for use with your own classes </li></ul><ul><li>Unlike other operators, if you don’t define the = operator, C++ provides a definition for you </li></ul><ul><li>If you want the = operator to do something other than assign each member, then you must create a customer operator=()function </li></ul><ul><li>In addition, if the class contains data fields that are pointers, you should create a custom function </li></ul>EX8-9
36. 36. Overloading [ ] and ( ) <ul><li>The subscript operator , operator[ ], is declared like any other function, but called in a manner similar to accessing an array element </li></ul><ul><li>You can include any instructions you want within an operator [ ] function </li></ul><ul><li>Typically, you use this function to perform a task that both requires an argument and does not quite fit into another operator’s usual meaning </li></ul>
37. 37. The Book Class Ex8-10