operator overloading

上传人:ji****72 文档编号:117191286 上传时间:2019-11-18 格式:PPT 页数:118 大小:485.50KB
返回 下载 相关 举报
operator overloading_第1页
第1页 / 共118页
operator overloading_第2页
第2页 / 共118页
operator overloading_第3页
第3页 / 共118页
operator overloading_第4页
第4页 / 共118页
operator overloading_第5页
第5页 / 共118页
点击查看更多>>
资源描述

《operator overloading》由会员分享,可在线阅读,更多相关《operator overloading(118页珍藏版)》请在金锄头文库上搜索。

1、Operator Overloading lOperator overloading like function overloading is another feature of C+ polymorphism lProgrammer can use some operator symbols to define special member functions of class lOperators provide programmers with a concise notation for expressing manipulations of objects of built-in

2、types. lOperators introduced for overloading should make either conventional or meaningful. Operator Overloading lWhy we need to have operator overloading? Make convenient to use in object operation. Example: int a=5,b=6, c; c=a+b; Can we do this? int ida100=2, idb100=3; int Idc100; idc=ida+idb; for

3、 (j=0;j are operator overloading built in C+ standard lib of iostream.h using operator “) operators lOverloaded in C+ library already for built- in data type lIt can also be overloaded for user defined data type lExample: / Fig. 8.3: fig08_03.cpp / Overloading the stream-insertion and / stream-extra

4、ction operators. #include using std:cout; using std:cin; using std:endl; using std:ostream; using std:istream; #include using std:setw; class PhoneNumber friend ostream private: char areaCode 4 ; / 3-digit area code and null char exchange 4 ; / 3-digit exchange and null char line 5 ; / 4-digit line

5、and null ; / Overloaded stream-insertion operator (cannot be / a member function if we would like to invoke it with / cout ( istream / skip ( input setw( 4 ) num.areaCode; / input area code input.ignore( 2 ); / skip ) and space input setw( 4 ) num.exchange; / input exchange input.ignore(); / skip da

6、sh (-) input setw( 5 ) num.line; / input line return input; / enables cin a b c; int main() PhoneNumber phone; / create object phone cout function by / issuing the call operator( cin, phone ). cin phone; / cout 0 ? arraySize : 10 ); ptr = new int size ; / create space for array assert( ptr != 0 ); /

7、 terminate if memory not allocated +arrayCount; / count one more object for ( int i = 0; i a.ptr i ; return input; / enables cin x y; / Overloaded output operator for class Array ostream for ( i = 0; i integers1 integers2; cout ( const String / test s1 length ) ) len = length - index; else len = sub

8、Length; / allocate temporary array for substring and / terminating null character char *tempPtr = new char len + 1 ; assert( tempPtr != 0 ); / ensure space allocated / copy substring into char array and terminate string strncpy( tempPtr, tempPtr len = 0; / Create temporary String object containing t

9、he substring String tempString( tempPtr ); delete tempPtr; / delete the temporary array return tempString; / return copy of the temporary String / Return string length int String:getLength() const return length; / Utility function to be called by constructors and / assignment operator. void String:s

10、etString( const char *string2 ) sPtr = new char length + 1 ; / allocate storage assert( sPtr != 0 ); / terminate if memory not allocated strcpy( sPtr, string2 ); / copy literal to object / Overloaded output operator ostream / buffer to store input input setw( 100 ) temp; s = temp; / use String class

11、 assignment operator return input; / enables cascading / Fig. 8.5: fig08_05.cpp / Driver for class String #include using std:cout; using std:endl; #include “string1.h“ int main() String s1( “happy“ ), s2( “ birthday“ ), s3; / test overloaded equality and relational operators cout = 1 / Preincrement

12、operator overloaded as a member function. Date return *this; / reference return to create an lvalue / Postincrement operator overloaded as a member function. / Note that the dummy integer parameter does not have a / parameter name. Date Date:operator+( int ) Date temp = *this; helpIncrement(); / ret

13、urn non-incremented, saved, temporary object return temp; / value return; not a reference return / Add a specific number of days to a date const Date i 9 ) temp.integer i %= 10; carry = 1; else carry = 0; return temp; / Addition HugeInt HugeInt:operator+( int op2 ) return *this + HugeInt( op2 ); / A

14、ddition HugeInt HugeInt:operator+( const char *op2 ) return *this + HugeInt( op2 ); ostream for ( i = 0; ( num.integer i = 0 ) i+ ) ; / skip leading zeros if ( i = 30 ) output 0; else for ( ; i = 29; i+ ) output num.integer i ; return output; / Fig. 8.8: fig08_08.cpp / Test driver for HugeInt class

15、#include using std:cout; using std:endl; #include “hugeint1.h“ int main() HugeInt n1( 7654321 ), n2( 7891234 ), n3( “99999999999999999999999999999“ ), n4( “1“ ), n5; cout “n1 is “ n1 “nn2 is “ n2 “nn3 is “ n3 “nn4 is “ n4 “nn5 is “ n5 “nn“; n5 = n1 + n2; cout n1 “ + “ n2 “ = “ n5 “nn“; cout n3 “ + “ n4 “n= “ ( n3 + n4 ) “nn“; n5 = n1 + 9; cout n1 “ + “ 9 “ = “ n5 “nn“; n5 = n2 + “10000“; cout n2 “ + “ “10000“ “ = “ n5 endl; return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号