Chapter 8Learning ObjectivesOperator Overloading IntroductionOperator Overloading PerspectiveOverloading BasicsOverloaded "+"Money "+" Definition: Display 8.1 Operator OverloadingOverloaded "=="Overloaded "==" for Money: Display 8.1 Operator OverloadingConstructors Returning ObjectsReturning by const ValueReturning by non-const ValueWhat to do with Non-const ObjectOverloading Unary OperatorsOverload "-" for MoneyOverloaded "-" DefinitionOverloaded "-" UsageOverloading as Member FunctionsMember Operator in Actionconst FunctionsOverloading Operators: Which Method?Overloading Function Application ()Other OverloadsFriend FunctionsSlide 25Friend Function UsesFriend Function PurityFriend ClassesReferencesReferences UsageReturning ReferenceReturning Reference in DefinitionOverloading >> and <<Overloading >>Slide 35Overloaded >> Return ValueOverloaded >> Example: Display 8.5 Overloading << and >> (1 of 5)Overloaded >> Example: Display 8.5 Overloading << and >> (2 of 5)Overloaded >> Example: Display 8.5 Overloading << and >> (3 of 5)Overloaded >> Example: Display 8.5 Overloading << and >> (4 of 5)Overloaded >> Example: Display 8.5 Overloading << and >> (5 of 5)Assignment Operator, =Increment and DecrementOverload Array Operator, [ ]Summary 1Summary 2Chapter 8Operator Overloading, Friends, and ReferencesCopyright © 2008 Pearson Addison-Wesley. All rights reservedLearning Objectives•Basic Operator Overloading–Unary operators–As member functions•Friends and Automatic Type Conversion–Friend functions, friend classes–Constructors for automatic type conversion•References and More Overloading–<< and >>–Operators: = , [], ++, --8-2Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Operator Overloading Introduction•Operators +, -, %, ==, etc.–Really just functions!•Simply "called" with different syntax:x + 7 –"+" is binary operator with x & 7 as operands–We "like" this notation as humans•Think of it as:+(x, 7)–"+" is the function name–x, 7 are the arguments –Function "+" returns "sum" of it’s arguments8-3Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Operator Overloading Perspective•Built-in operators–e.g., +, -, = , %, ==, /, *–Already work for C++ built-in types–In standard "binary" notation•We can overload them!–To work with OUR types!–To add "Chair types", or "Money types"•As appropriate for our needs•In "notation" we’re comfortable with•Always overload with similar "actions"!8-4Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overloading Basics•Overloading operators–VERY similar to overloading functions–Operator itself is "name" of function•Example Declaration:const Money operator +( const Money& amount1, const Money& amount2);–Overloads + for operands of type Money–Uses constant reference parameters for efficiency–Returned value is type Money•Allows addition of "Money" objects8-5Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overloaded "+"•Given previous example:–Note: overloaded "+" NOT member function–Definition is "more involved" than simple "add"•Requires issues of money type addition•Must handle negative/positive values•Operator overload definitions generallyvery simple–Just perform "addition" particular to "your" type8-6Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Money "+" Definition: Display 8.1 Operator Overloading•Definition of "+" operator for Money class:8-7Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overloaded "=="•Equality operator, ==–Enables comparison of Money objects–Declaration:bool operator ==(const Money& amount1,const Money& amount2);•Returns bool type for true/false equality–Again, it’s a non-member function(like "+" overload)8-8Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overloaded "==" for Money:Display 8.1 Operator Overloading•Definition of "==" operator for Money class:8-9Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Constructors Returning Objects•Constructor a "void" function?–We "think" that way, but no–A "special" function•With special properties•CAN return a value!•Recall return statement in "+" overloadfor Money type:–return Money(finalDollars, finalCents);•Returns an "invocation" of Money class!•So constructor actually "returns" an object!•Called an "anonymous object"8-10Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Returning by const Value•Consider "+" operator overload again:const Money operator +(const Money& amount1, const Money& amount2);–Returns a "constant object"?–Why?•Consider impact of returning "non-const"object to see…8-11Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Returning by non-const Value•Consider "no const" in declaration:Money operator +( const Money& amount1, const Money& amount2);•Consider expression that calls:m1 + m2–Where m1 & m2 are Money objects–Object returned is Money object–We can "do things" with objects!•Like call member functions…8-12Copyright © 2008 Pearson Addison-Wesley. All rights reserved.What to do with Non-const Object•Can call member functions:–We could invoke member functions onobject returned by expression m1+m2:•(m1+m2).output(); //Legal, right?–Not a problem: doesn’t change anything•(m1+m2).input(); //Legal!–PROBLEM! //Legal, but MODIFIES!•Allows modification of "anonymous" object!•Can’t allow that here!•So we define the return object as const8-13Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overloading Unary Operators•C++ has unary operators:–Defined as taking one operand–e.g., - (negation)•x = -y; // Sets x equal to negative of y–Other unary operators:•++, --•Unary operators can also be overloaded8-14Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overload "-" for Money•Overloaded "-" function declaration–Placed outside class definition:const Money operator –(const Money& amount);–Notice: only one argument•Since only 1 operand (unary)•"-" operator is overloaded twice!–For two operands/arguments (binary)–For one operand/argument (unary)–Definitions must exist for both8-15Copyright © 2008 Pearson Addison-Wesley. All rights reserved.Overloaded "-" Definition•Overloaded "-" function definition:const Money operator –(const Money& amount){return Money(-amount.getDollars(), -amount.getCents());}•Applies "-"
View Full Document