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.> Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-11 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.
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. Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-12 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
What to do with Non-const Object Can call member functions: We could invoke member functions on object returned by expression m1+m2: (m1+m2).output();//Legal,right? Not a problem:doesn't change anything ◆(m1+m2).input(0; //Legal! ◆PROBLEM! //Legal,but MODIFIES! ◆Allows modification of"anonymous"object赳 ◆Can't allow that here! So we define the return object as const Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-13 What to do with Non-const Object ¨ Can call member functions: ¨ We could invoke member functions on object 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 const
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 overloaded Copyright006 Pearson Addison-Wesley.All rights reserved. 8-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-14 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 overloaded
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 both Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-15
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-15 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 both