下載講義

advertisement
計算機程式
第十一單元 Operator Overloading I
授課教師:廖婉君教授
【本著作除另有註明外,採取創用CC「姓名標示
-非商業性-相同方式分享」台灣3.0版授權釋出】
本課程指定教材為 C++ How to Program, 7/e, Harvey M.
Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. © 2010。
本講義僅引用部分內容,請讀者自行準備。
本作品轉載自Microsoft Office 2007多媒體藝廊,依據Microsoft服務合約及著作權
法第46、52、65條合理使用。
1
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
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
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
object2 = object2.add( object1 );
o
vs.
object2 = object2 + object1;
4
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
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
Outline
• PhoneNumber.h
• (1 of 1)
p.472
7
Outline
• PhoneNumber.cpp
• (1 of 2)
p.472-473
8
Outline
• PhoneNumber.cpp
• (2 of 2)
p.472-473
9
Outline
• fig11_05.cpp
• (1 of 1)
p.473-474
10
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
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
Case Study: Array Class
• Pointer-based arrays in C++
o
o
o
o
No range checking
Cannot be compared meaningfully with ==
No array assignment (array names are const pointers)
If array passed to a function, size must be passed as a separate argument
• Example: Implement an Array class with
o
o
o
o
o
Range checking
Array assignment
Arrays that know their own size
Outputting/inputting entire arrays with << and >>
Array comparisons with == and !=
13
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
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
版權聲明
頁碼
作品
版權圖示
來源/作者
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。
16
Download