Chapter 8 ABSOLUTE C++ Operator Overloading, Friends, and References WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 8 Operator Overloading, Friends, and References
Learning 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:=,l,++,- Copyright006 Pearson Addison-Wesley.All rights reserved. 8-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-2 Learning 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: = , [], ++ , -
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 arguments Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-3 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 arguments
Operator Overloading Perspective ◆Built-in operators ◆e.g,+,=,%,=,1,* Already work for C++built-in types In standard "binary"notation ◆Ve 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"! Copyright Pearson Addison-Wesley.All rights reserved. 8-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-4 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"!
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"objects Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-5 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" objects