Presentation is loading. Please wait.

Presentation is loading. Please wait.

計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.

Similar presentations


Presentation on theme: "計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to."— Presentation transcript:

1 計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. © 2010 。 本 講義僅引用部分內容,請讀者自行準備。 1 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法 第 46 、 52 、 65 條合理使用。 Microsoft 服務合約

2 Introduction Function overloading vs. operator overloading e.g., int a, b; e.g., double c, d; a=a+b; c=c+d; o +(int, int); +(double, double); etc. o add(a, b); o opeator+(a, b); 2

3 Introduction (cont.) Types for operator overloading o Built in (int, char) or user-defined (classes) o Can use existing operators with user-defined types Cannot create new operators How to overload an operator for a class? o Create a function for the class o Name of operator function Keyword operator followed by symbol o Example operator+ for the addition operator + 3

4 Introduction (cont.) Using operators on a class object o It must be overloaded for that class Exceptions: (can also be overloaded by the programmer) o Assignment operator (=) Memberwise assignment between objects o Address operator (&) Returns address of object o Comma operator (,) Overloading provides concise and familiar notation o object2 = object2.add( object1 ); vs. object2 = object2 + object1; 4

5 Introduction (cont.) Cannot change.. o Precedence of operator (order of evaluation) Use parentheses to force order of operators o Associativity (left-to-right or right-to-left) o Number of operands (arity) e.g., & is unary, can only act on one operand o How operators act on built-in data types (i.e., cannot change integer addition) Cannot create new operators Operators must be overloaded explicitly o Overloading + and = does not overload += Operator ?: cannot be overloaded 5

6 Introduction (cont.) Operator functions o As for member functions Leftmost object must be of same class as operator function Use this pointer to implicitly get left operand argument Operators (), [], -> or any assignment operator must be overloaded as a class member function Called when o Left operand of binary operator is of this class o Single operand of unary operator is of this class o As for global functions Need parameters for both operands Can have object of different class than operator Can be a friend to access private or protected data 6

7 Outline PhoneNumber.h (1 of 1) 7 p.472

8 Outline PhoneNumber.cpp (1 of 2) 8 p.472-473

9 Outline PhoneNumber.cpp (2 of 2) 9 p.472-473

10 Outline fig11_05.cpp (1 of 1) 10 p.473-474

11 Overloading Unary Operators Upcoming example (Section 11.10) o Overload ! to test for empty string, !s o If non-static member function, needs no arguments class String { public: bool operator!() const; … }; !s becomes s.operator!() o If global function, needs one argument bool operator!( const String & ) !s becomes operator!(s) 11

12 Overloading Binary Operators Upcoming example: Overloading + o If non-static member function, needs one argument class Time { public: const Time operator+(const Time &) const; … }; y + z becomes y.operator+( z ) o If global function, needs two arguments const Time operator+(const Time &, const Time & ); y + z becomes operator+(y,z) 12

13 Case Study: Array Class Pointer-based arrays in C++ o No range checking o Cannot be compared meaningfully with == o No array assignment (array names are const pointers) o If array passed to a function, size must be passed as a separate argument Example: Implement an Array class with o Range checking o Array assignment o Arrays that know their own size o Outputting/inputting entire arrays with > o Array comparisons with == and != 13

14 Dynamic memory allocation new and delete e.g., int *aptr, *bptr; aptr = new int; aptr = new int(3); delete aptr; bptr = new int [20]; delete [] bptr; e.g., Time *tptr; tptr = new Time; tptr = new Time(1,2,3); 14

15 Case Study: Array Class (cont.) Copy constructor o Used whenever copy of object is needed: Passing by value (return value or parameter) Initializing an object with a copy of another of same type o Array newArray( oldArray ); or o Array newArray = oldArray o Prototype for class Array Array( const Array & ); Must take reference 15

16 版權聲明 16 頁碼作品版權圖示來源 / 作者 1-16 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。 7-10 Open Clip Art Library ,作者: aritztg ,本作品轉載自: http://openclipart.org/detail/3422/mouse-by-aritztg ,瀏覽日期: 2013/1/10 。 http://openclipart.org/detail/3422/mouse-by-aritztg


Download ppt "計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to."

Similar presentations


Ads by Google