中国高校课件下载中心 》 教学资源 》 大学文库

《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References

文档信息
资源类别:文库
文档格式:PPT
文档页数:46
文件大小:879.5KB
团购合买:点击进入团购
内容简介
《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References
刷新页面文档预览

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 。> ◆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 ¨ > ¨ 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

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 generally very simple Just perform "addition"particular to "your"type Copyright006 Pearson Addison-Wesley.All rights reserved. 8-6

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-6 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 generally very simple ¨ Just perform "addition" particular to "your" type

Money "+Definition: Display 8.1 Operator Overloading ◆ Definition of "+"operator for Money class: 52 const Money operator +(const Money&amount1,const Money&amount2) 53 54 int allCents1 amount1.getCents()+amount1.getDollars()*100; 5 int allCents2 amount2.getCents()+amount2.getDollars()*100; 56 int sumAllCents allCents1 allCents2; 57 int absAllCents abs(sumAllCents);//Money can be negative. 5 int finalDollars absAllCents/100; 59 int finalCents absAllCents%100; 60 if (sumAllCents 0) If the return 61 statements finalDollars =-finalDollars; puzzle you,see 63 finalCents =-finalCents; the tip entitled 6 A Constructor Can Return an 65 return Money(finalDollars,finalCents); Object. 66 2 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-7

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-7 Money "+" Definition: Display 8.1 Operator Overloading ¨ Definition of "+" operator for Money class:

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) Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-8

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-8 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)

Overloaded "==for Money: Display 8.1 Operator Overloading Definition of "=="operator for Money class: 83 bool operator ==(const Money&amount1,const Money&amount2) 84 85 return ((amount1.getDollars()==amount2.getDollars()) 86 &(amount1.getCents()=amount2.getCents())) 87 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-9

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-9 Overloaded "==" for Money: Display 8.1 Operator Overloading ¨ Definition of "==" operator for Money class:

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 "+"overload for Money type: return Money(finalDollars,finalCents); ◆Returns an"invocation'"of Money class! So constructor actually "returns"an object! Called an "anonymous object" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 8-10

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 8-10 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 "+" overload for Money type: ¨ return Money(finalDollars, finalCents); ¨ Returns an "invocation" of Money class! ¨ So constructor actually "returns" an object! ¨ Called an "anonymous object

刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档