Unformatted text preview:

Overloading OperatorsObjectivesSlide 3Understanding the Benefits of OverloadingUsing the + Operator PolymorphicallyOverloading Operators— The RulesSlide 7Binary Operators that Can Be OverloadedSlide 9Slide 10Overloading Math OperatorsSlide 12Slide 13Slide 14Paying Attention to the Order of the OperandsSlide 16Output of SalesOffice2.cppOverloading an Operator to Work with a Class Object and a Primitive TypeSlide 19Using Multiple Operations in a StatementThe Sale ClassSlide 22Program that Adds Three Sale ObjectsSlide 24Slide 25Overloading OutputSlide 27Slide 28Slide 29Overloading InputOverloaded Operator>>() Function for the Sale ClassSlide 32Overloading ++ and - -Using the Prefix and Postfix ++ Operators with an IntegerThe Inventory ClassSlide 36Using Postfix Increment and Decrement OperatorsOverloading the = = OperatorSlide 39Overloading the = OperatorThe Movie ClassProgram that Uses the Movie ClassSlide 43Slide 44Overloading [ ] and ( )The Book ClassSlide 47Using the Parentheses OperatorSlide 49SummarySlide 51Slide 52Overloading OperatorsObject-Oriented Programming Using C++Second Edition8Objectives•In this chapter, you will learn:•About the benefits of overloading•About the rules that apply to operator overloading•How to overload math operators•How to overload operators to work with a class object and a primitive object•How to chain multiple mathematical operations in a statement8Objectives•In this chapter, you will learn:•How to overload the insertion (<<) operator for output•How to overload the extraction operator (>>) for input•How to overload the prefix and postfix ++ and -- operators•How to overload the = = operator•How to overload the = operator•How to overload the subscript and parentheses operators8Understanding the Benefits of Overloading•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•Polymorphism allows the same operation to be carried out differently, depending on the object•Purists find a subtle difference between overloading and polymorphism•Some reserve the term polymorphism (or pure polymorphism) for situations in which one function body is used with a variety of arguments8Using the + Operator Polymorphically•Separate actions can result from what seems to be the same operation or command•The + operator has a variety of meanings, which include:–Alone before a value (called unary form), + indicates a positive values, as in the expression +7–Between two integers (called binary form), + indicates integer addition, as in the expression 5+ 9–Between two floating-point numbers (also called binary form), + indicates floating-point addition, as in the expression 6.4 + 2.18Overloading Operators—The Rules•Operator overloading is the process by which you apply operators to your own abstract data types•The +, -, *, and / symbols make it easy to work with built-in data types such as int and double•Classes, however, contain a variety of data members•As a result, if you want the compiler to perform arithmetic with two class objects, you must tell the compiler what you mean•Good programming style dictates that you endow the operator with a reasonable meaning8Overloading Operators—The Rules•You overload an operator by making it a function; subsequently, you can use it just like any other function•C++ operators are classified as unary or binary, depending on whether they take one or two arguments, respectively8Binary Operators that Can Be Overloaded8Overloading Operators—The Rules•Associativity refers to the order in which actions within an expression are carried out•You cannot change associativity when you overload operators•You also cannot change the normal precedence of any operator8Overloading Operators—The Rules8Overloading Math Operators•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•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•The name of the operator function that overloads the + symbol is operator+()8Overloading Math Operators8Overloading Math Operators•The operator+() function in Figure 8-1 can work like any other member function •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•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:sum = clerk + driver;sum = driver + clerk;8Overloading Math Operators8Paying Attention to the Order of the Operands•You can choose to overload any of the arithmetic operators for any classes you develop•Then you can use the corresponding operator symbol in a natural way with class objects8Paying Attention to the Order of the Operands•In the steps outlined on pages 270 to 282 of the textbook, you create a class for a SalesOffice•The class will include an overloaded division operator (operator /) so you can divide one office’s sales by another to determine the ratio of their sales8Output of SalesOffice2.cpp8Overloading an Operator to Work with a Class Object and a Primitive Type•When you add two objects using the + operator, the objects do not have to be the same type•You can add an integer and a double with an expression such as 5 + 7.848Overloading an Operator to Work with a Class Object and a Primitive Type•You cannot overload operators that work with C++’s built-in data types•You cannot overload the + that works with two doubles to make it do anything but add two doubles•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 object8Using Multiple Operations in a Statement•Most modern programming languages allow several operators to be used in the same statement•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•Then, in a separate statement, you add the third value to that total8The Sale Class8Using Multiple Operations in a Statement•Because the associativity of addition occurs from


View Full Document

WPU CS 2300 - Overloading Operators

Download Overloading Operators
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Overloading Operators and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Overloading Operators 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?