Introduction Function overloading vs operator overloading e g

  • Slides: 16
Download presentation

Introduction • Function overloading vs. operator overloading e. g. , int a, b; e.

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

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

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 object 2 = object 2. add( object 1 ); o vs. object 2 = object 2 + object 1; 4

Introduction (cont. ) • Cannot change. . o Precedence of operator (order of evaluation)

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

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 • Phone. Number. h • (1 of 1) p. 472 7

Outline • Phone. Number. h • (1 of 1) p. 472 7

Outline • Phone. Number. cpp • (1 of 2) p. 472 -473 8

Outline • Phone. Number. cpp • (1 of 2) p. 472 -473 8

Outline • Phone. Number. cpp • (2 of 2) p. 472 -473 9

Outline • Phone. Number. cpp • (2 of 2) p. 472 -473 9

Outline • fig 11_05. cpp • (1 of 1) p. 473 -474 10

Outline • fig 11_05. cpp • (1 of 1) p. 473 -474 10

Overloading Unary Operators • Upcoming example (Section 11. 10) o Overload ! to test

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

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 No range checking

Case Study: Array Class • Pointer-based arrays in C++ 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 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

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

Case Study: Array Class (cont. ) • Copy constructor o Used whenever copy of

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 new. Array( old. Array ); or o Array new. Array = old. Array o Prototype for class Array • Array( const Array & ); • Must take reference 15

版權聲明 頁碼 作品 版權圖示 來源/作者 1 -16 本作品轉載自Microsoft Office 2007多媒體藝廊,依據Microsoft服務合約及著作權法第 46、52、65條合理使用。 7 -10 Open

版權聲明 頁碼 作品 版權圖示 來源/作者 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