11 2
play

11.2 Overloading Operators Overloading Operators In the Money - PowerPoint PPT Presentation

11.2 Overloading Operators Overloading Operators In the Money class, function add was used to add two objects of type Money In this section we see how to use the ' + ' operator to make the following code legal: Money


  1. 11.2 Overloading Operators

  2. 
 Overloading Operators ■ In the Money class, function add was used to 
 add two objects of type Money 
 ■ In this section we see how to use the ' + ' operator 
 to make the following code legal: 
 Money total, cost, tax; 
 … 
 total = cost + tax; 
 // instead of total = add(cost, tax);

  3. Operators As Functions ■ An operator is a function used differently than 
 an ordinary function ■ An ordinary function call enclosed its arguments in 
 parenthesis 
 add(cost, tax) 
 ■ With a binary operator, the arguments are on either 
 side of the operator 
 cost + tax

  4. Operator Overloading ■ Operators can be overloaded ■ The definition of operator + for the Money 
 class is nearly the same as member function add ■ To overload the + operator for the Money class ■ Use the name + in place of the name add ■ Use keyword operator in front of the + ■ Example: friend Money operator + (const Money& amount1, const Money& amount2)

  5. Operator Overloading Rules ■ At least one argument of an overloaded operator 
 must be of a class type ■ An overloaded operator can be a friend of a class ■ The number of arguments for an operator cannot 
 be changed ■ The precedence of an operator cannot be changed ■ . , :: , * , and ? cannot be overloaded

  6. Program Example: 
 Overloading Operators � � ■ The Money class with overloaded operators 
 + and == is demonstrated in Display 11.5 (1) Display 11.5 (2)

  7. Display 11.5 
 (1/2)

  8. Display 11.5 (2/2) 


  9. Automatic Type Conversion ■ With the right constructors , the system can do 
 type conversions for your classes ■ The following code (from Display 11.5) actually works 
 Money base_amount(100, 60), full_amount; 
 full_amount = base_amount + 25; 
 ■ The integer 25 is converted to type Money so it 
 can be added to base_amount! ■ How does that happen?

  10. Type Conversion Event 1 ■ When the compiler sees base_amount + 25, 
 it first looks for an overloaded + operator to 
 perform 
 Money_object + some-integer 
 ■ If it exists, it might look like this 
 friend Money operator +(const Money& amount1, 
 const int& amount2);

  11. Type Conversion Event 2 ■ When the appropriate version of + is not found, 
 the compiler looks for a constructor that takes 
 a single integer 
 ■ The Money constructor that takes a single parameter of type long will work 
 ■ The constructor Money(long dollars) converts 25 
 to a Money object so the two values can be added!

  12. 
 Type Conversion Again ■ Although the compiler was able to find a 
 way to add 
 base_amount + 25 
 this addition will cause an error 
 base_amount + 25.67 
 ■ There is no constructor in the Money class that takes a single argument of type double

  13. 
 A Constructor For double ■ To permit base_amount + 25.67, the following 
 constructor should be declared and defined 
 class Money 
 { 
 public: 
 … 
 Money(double amount); 
 // Initialize object so its value is $amount 
 …

  14. Overloading Unary Operators ■ Unary operators take a single argument ■ The unary – operator is used to negate a value 
 x = -y 
 ■ ++ and - - are also unary operators ■ Unary operators can be overloaded ■ The Money class of Display 11.6 can include ■ A binary – operator ■ A unary – operator

  15. Overloading - ■ Overloading the – operator with two parameters 
 allows us to subtract Money objects as in 
 Money amount1, amount2, amount2; 
 … 
 amount3 = amount1 – amount2; ■ Overloading the – operator with one parameter 
 allows us to negate a money value like this 
 amount3 = - amount1; Display 11.6

  16. Display 
 11.6 


  17. 
 Overloading << and >> ■ The insertion operator << is a binary operator ■ The first operand is the output stream ■ The second operand is the value following << 
 cout << "Hello out there.\n"; Operand 1 Operand 2 Operator

  18. Replacing Function output ■ Overloading the << operator allows us to 
 use << instead of Money's output function ■ Given the declaration: Money amount(100); 
 
 amount.output( cout ); 
 can become 
 cout << amount;

  19. 
 What Does << Return? ■ Because << is a binary operator 
 cout << "I have " << amount << " in my purse."; 
 seems as if it could be grouped as 
 ( (cout << "I have" ) << amount) << "in my purse."; 
 ■ To provide cout as an argument for << amount, 
 (cout << "I have") must return cout Display 11.7

  20. Display 11.7 


  21. Overloaded << Declaration Based on the previous example, << should return 
 ■ its first argument, the output stream This leads to a declaration of the overloaded 
 ■ << operator for the Money class: 
 
 class Money 
 { 
 public: 
 … 
 friend ostream& operator << (ostream& outs, 
 const Money& amount); 
 …

  22. Overloaded << Definition ■ The following defines the << operator ostream operator <<(ostream& outs, 
 const Money& amount) 
 { 
 <Same as the body of Money::output in 
 Display 11.3 (except all_cents is replaced 
 with amount.all_cents) > 
 
 return outs; 
 }

  23. Return ostream& ? ■ The & means a reference is returned ■ So far all our functions have returned values ■ The value of a stream object is not so simple to 
 return ■ The value of a stream might be an entire file, the 
 keyboard, or the screen! ■ We want to return a reference to the stream , not the value of the stream ■ The & means that we want to return a reference to the stream, not its value

  24. Overloading >> ■ Overloading the extraction >> operator for input is very similar to overloading the << for output ■ >> could be defined this way for the Money class 
 � istream& operator >>(istream& ins, Money& amount) { 
 <This part is the same as the body of 
 � Money::input in Display 11.3 (except that 
 all_cents is replaced with amount.all_cents)> 
 � � � � return ins; 
 } Display 11.8 (1-4)

  25. Display 11.8 
 (1/4)

  26. Display 11.8(2/4) 


  27. Display 11.8 (3/4) 


  28. Display 11.8 (4/4) 
 File input and output will be discussed soon.

  29. Section 11.2 Exercises ■ Can you 
 ■ Describe the purpose of a making a function a friend? 
 ■ Describe the use of constant parameters? 
 ■ Identify the return type of the overloaded operators << and >>?

  30. 11.3 Arrays and Classes

  31. 
 Arrays and Classes ■ Arrays can use structures or classes as their 
 base types ■ Example: struct WindInfo 
 { 
 double velocity; 
 char direction; 
 } 
 WindInfo data_point[10];

  32. Accessing Members ■ When an array's base type is a structure or a 
 class… Use the dot operator to access the members of an indexed ■ variable Example: for (i = 0; i < 10; i++) 
 ■ { 
 cout << "Enter velocity: "; 
 cin >> data_point[i].velocity; 
 … 
 }

  33. An Array of Money ■ The Money class of Chapter 11 can be the base 
 type for an array � ■ When an array of classes is declared ■ The default constructor is called to initialize the indexed variables ■ An array of class Money is demonstrated in 
 Display 11.9 (1-3)

  34. Display 11.9 (1/3) 


  35. Display 11.9 (2/3) 


  36. Display 11.9 
 (3/3)

  37. 
 Arrays as Structure Members ■ A structure can contain an array as a member ■ Example: struct Data 
 { 
 double time[10]; 
 int distance; 
 } 
 Data my_best; ■ my_best contains an array of type double

  38. Accessing Array Elements ■ To access the array elements within a structure ■ Use the dot operator to identify the array within the structure ■ Use the [ ]'s to identify the indexed variable desired ■ Example: my_best.time[i] 
 references the i-th indexed variable of the variable time in the structure my_best

  39. Arrays as Class Members ■ Class TemperatureList includes an array ■ The array, named list, contains temperatures ■ Member variable size is the number of items stored class TemperatureList 
 { 
 public: 
 TemperatureList( ); 
 //Member functions 
 private: 
 double list [MAX_LIST_SIZE]; // the allocated memory?? 
 int size; 
 }

  40. Overview of TemperatureList ■ To create an object of type TemperatureList: TemperatureList my_data; ■ To add a temperature to the list: My_data.add_temperature(77); ■ A check is made to see if the array is full ■ << is overloaded so output of the list is Display 11.10 (1-2) cout << my_data;

  41. Display 11.10 (1/2) 
 size is also used for next potentially available position in the array.

  42. Display 11.10 
 (2/2)

  43. Section 11.3 Conclusion ■ Can you 
 ■ Declare an array as a member of a class? ■ Declare an array of objects of a class? ■ Write code to call a member function of an element in an array of objects of a class? ■ Write code to access an element of an array of integers that is a member of a class?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend