精品课件浙大魏宝刚面向对象程序设计c  基础第八_第1页
精品课件浙大魏宝刚面向对象程序设计c  基础第八_第2页
精品课件浙大魏宝刚面向对象程序设计c  基础第八_第3页
精品课件浙大魏宝刚面向对象程序设计c  基础第八_第4页
精品课件浙大魏宝刚面向对象程序设计c  基础第八_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第八章 运算符重载 用户定义类型用运算符表示操作 8.1 运算符重载 运算符一种函数;对于类对象,用户可重新定义运算符函数。 class Complex private: double rpart; double ipart; public: Complex ( ) rpart=ipart=0.0; Complex(double rp, double ip) rpart=rp; ipart=ip; Complex add(const Complex temp.rpart=com.rpart+rpart; temp.ipart=com.ip+ipart; return temp; ; Complex a(10,7), b(3,5); Complex c=a.add(b); 希望c=a+b; 复数加法运算重载符函数: class Complex / public: Complex operator+(Complex return temp; / ; Complex a(10,7), b(3,5), c; c=a+b; 赋值运算符一般 用预定义的 a.operator(b) 公有成员函数 单目运算符重载: Complex Complex: operator - ( ) Complex temp; temp.rpart=-rpart; temp.ipart)=-ipart; return temp; 单目无参数 双目一个参数 Complex c=-a+b; 不能重载的运算符:. , * , : , ? , sizeof (a.operator-( ).operator+(b) 重载应注意的问题: 1、优先级和结合顺序不变 2、不能改变目数 3、类外定义至少有一个相应类的参数 class x public: int i; ; X operator+(const X temp.i=x1.i+x2.i; return temp; 外部函数,若写为: X operator +(int i1,int i2) 内部类型,用户不能改变 运算符成员函数调用成员函数的对象隐式成为表达式的第一个运算数 外部运算符函数其第一个参数直接对应表达式的第一个运算数; x1=x2+x3; operator+(x2, x3); 4、双目运算符:成员函数只带一个参数;外部函数带两个参数 aabb aabb 5、单目前缀运算符:成员函数不带参;外部函数带一个参数 aa aa aa.operator(bb) operator(aa, bb) aa.operator( ) operator(aa) 6、单目后缀运算符:成员函数带一个int参数;外部函数带二个参数 aa aa aa.operator( int ) operator(aa, int) 7、只能重载已有的运算符,不能创造新的 8、成员和外部运算符函数可重载(多个同名运算符函数) class x public: double operator+(double) / double operator+(int) / ; float operator+(X, int) / X x1, x2, x3; x2+3.0; x2+3; 9、编译器预定义了“=”, “ void operator void operator, (const X / ; void f(X a, X b) a=b; X *p= 说明为私有限制对它们的使用 8.2 算术运算符、赋值运算符和逻辑运算符的重载 8.2.1 重载算术运算符 class Complex / public: Complex operator+(double) / / ; Complex a, b; a=b+10.0; a=10.0+b; b.operator(10.0) 10.0.operator(b) 解决此问题非成员函数重载: Complex operator+(double d, const Complex temp.rpart=d+rpart; temp.ipart=com.ipart; return temp; 问题?解决方法? 用于Complex的其它算术运算符重载: class Complex / friend Complex operator *(double, Complex com); public: Complex operator*(double d) / / ; Comple operator *(double d, Complex com) / Complex a, b, c; a=3*b; a=b+c*3*5; 8.2.2 重载赋值运算符 编译器缺省的赋值运算符是逐位把源对象拷贝到目标对象。 Complex c1, c2; c1=c2; 类Cmessage的问题: #include extern “C” #include class CMessage private: char *buffer; public: CMessage( ) buffer=new char(0); CMessage( ) delete buffer; void Display( ) cout(const Complex / ; int operator (const Complex 注意:多种类型进行混合运算的运算符成员函数一般采用外部友员函数的形式。 其他用到转换构造函数的地方: Complex f( ) return 1.0; 8.3.2 转换运算符 类对象类型(X) 别的类型(T) 转换运算符(成员函数)无参数、无返回值 X: operator T( ) / class integer int i; public: integer(int a) i=a; operator int( ) return i; ; integer i1(10), i2(20); int a=i1; i1=a; i2=10+i1*2 可采用强制(显式)类型转换形式: int i; integer ir(10); i=int(ir); ir=integer(i); 类型 8.3.3 二义性 同时定义转换运算符和转换构造函数: class X int i; public: friend X operator+(const X X(int ii) i=ii; operator int( ) return i; ; X operator +(const X X x1(10); int i=x1+12; i=12+x1; 8.5 重载增量和减量运算符 class integer int i; public: integer( int ii) i=ii; integer operator+( ) +i; return *this; integer operator+(int) i+; return *this; ; integer a(1), b(1); b=a+; b=+a; 运算符名成员函数形式外部函数形式调用形式等价调用 前缀+X operator+( )X operator+( X +i; return temp; 8.6 重载下标运算符 数组名下标:双目运算符。 注意:下标运算符函数不能用外部函数定义,只能非静态成员函数 一般形式: T1 T:operator (T2); class ainteger int *a; int sz; public: ainteger( int size) sz=size; a=new intsize; int if(free=max) pair * nvec=new pairmax*2; for(int i=0; iname=new charstrlen(p)+1; strcpy(pp-name, p); pp-val=0; return pp-val; assoc:assoc(int ) max=(sbuf) vecbuf+; vec.print_all( ); assoc vec(512); / coutfree? ; void main( ) const MAX=256; char bufMAX; assoc vec(512); while(cinbuf) vecbuf+; assoc_iterator next(vec); pir *p; while(p=next( ) coutnameval运算符函数返回是中间结果,在此结果上施加内定义的-运算。 class B; class A public: B* operator-( ); / ; class B / ; void main( ) A a; a-b 举例: class A private: char *cp; public: void a( ); void b( ); void c( ); A(char *r) cp=new charstrlen(r)+1; strcpy(cp,r); ; 1、在对象a上执行A:operator-( ) 2、返回值放入类型为B* 的临时对象x中 3、求x-b得结果 A的运算符函数返回B的指针, 再通过内定义函数访问B的成员 (a.operator-( )-b class B private: A *delegate; public: void a( ) delegate-a( ); void b( ) delegate-b( ); void c( ) delegate-c( ); void d( ); B(char *r=0) delegate=new A(r); ; 解决方法(适用非运算符重载函数)B中重载-运算符: class B private: A *delegate; public: void d( ); A* operator-( ) return delegate; B(char *r=0) delegate=new A(r); ; B b; b.d( ); b-a( ); b-b( ); b-c( ); 注意:b并不是指针 仅为了重用A中的方法 ,B要重新定义函数 习题: 1、写出程序执行结果: #include class myclass int i; float j; public: myclass(int x=0, float y=0) i=x; j=y; myclass operator( ) (int k, float m); void display( ) cout #include class Name public: Name( ) pName=0; Name(char *pn) copyName(pn); Name(Name Name( ) deleteName( ); Name =( Name copyName(s.pName); return ; void display( ) coutpNameendl; protected: void copyName(char *pN); void ; char *pName; ; void copyName(char * pN) pName=new ; ; void deleteName( ) delete ; int row,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论