chap6_运算符重载.ppt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- chap6_ 运算 重载
- 资源描述:
-
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,算术运算符:加(,+,)、减,string s1,s2;/s1,s2,为字符串对象,s1+s2/?,CComplex,a,b,;/,复数类对象,a+b,/?,a-b/?,x+y/x,y,为,int,float,double,char,.,问题引出:,运算符重载,#include,class,CComplex,public:,CComplex(double,rt,=0,double it=0);,void,disp,();,void,set(double,double,);,void,get(double&,double,CComplex,add(CComplex,CComplex,dec(CComplex,private:,double,r,i,;,;,CComplex:CComplex(double,rt,double,it),r=,rt;i,=it;,void,CComplex:set(double,rt,double,it),r=,rt;i,=it;,void,CComplex:get(double,&,rt,double,&it),rt,=,r;it,=i;,void,CComplex:disp,(),if(,r|r,=0&i=0),cout,0&r),cout,+;,if(i),if(i=1);,else if(i=-1),cout,-;,else,cout,i;,cout,i;,cout,endl,;,CComplex,CComplex:add(CComplex,&t),return,CComplex(r+t.r,i+t.i,);,CComplex,CComplex:dec(CComplex,&t),return,CComplex(r-t.r,i-t.i,);,void main(),CComplex,a(10,10),b(15,15),c;,a.disp,();,b.disp,();,c=,a.add(b,);,c.disp,();,c=,a.dec(b,);,c.disp,();,10+10i,15+15i,25+25i,-5-5i,c=,a+b,;,c=a-b;,complex1,问题引出:自定义类型的运算,void main(),CComplex,a(10,10),b(15,15),c;,a.disp,();,b.disp,();,c=,a+b,;,c.disp,();,c=a-b;,c.disp,();,编译器将其解释为函数调用:,c=,a.,operator+,(b,);,c=,a.,operator-,(b,);,定义了两个重载运算符的成员函数,它们的名字是:,operator+,operator,complex2,#include,class,CComplex,public:,CComplex(double,rt,=0,double it=0);,void,disp,();,void,set(double,double,);,void,get(double&,double,CComplex,operator+,(,CComplex,CComplex,operator-,(,CComplex,private:,double,r,i,;,;,CComplex:CComplex(double,rt,double,it),r=,rt;i,=it;,void,CComplex:set(double,rt,double,it),r=,rt;i,=it;,void,CComplex:get(double,&,rt,double,&it),rt,=,r;it,=i;,void,CComplex:disp,(),if(,r|r,=0&i=0),cout,0&r),cout,+;,if(i),if(i=1);,else if(i=-1),cout,-;,else,cout,i;,cout,i;,cout,endl,;,CComplex,CComplex:,operator,+,(,CComplex,&t),return,CComplex(r+t.r,i+t.i,);,CComplex,CComplex:,operator,-,(,CComplex,&t),return,CComplex(r-t.r,i-t.i,);,#include,class,CPoint,public:,CPoint(int,xx=0,int,yy,=0),x=xx;y=,yy,;,void,disp,(),cout,(x,y)”,既作为流的插入运算符,又作为移位运算符,根据其操作对象的不同,执行其不同的功能。,在,c+,中,允许对大多数的运算符进行重载,通过,定义重载运算符的函数,使它能够用于特定类的对象。,运算符重载的实现,运算符重载实际上是将运算符重载为一个函数,在重载某个运算符时,实际上就是定义一个,重载运算符的函数,,函数名为,operator,运算符,。在执行被重载的运算符时,系统自动调用该函数以实现相应的运算,对表达式,a+b,、,ab,,编译器将其解释为函数调用:,a.operator,+(b);,a.operator,(b);,运算符重载是通过定义函数实现的,运算符重载实质上是函数的重载(遵循函数重载的原则)。,对于重载的运算符可以使用运算符方式调用,也可以使用函数调用方式调用。,CComplex,CComplex:,operator,+,(,CComplex,&t),return,CComplex(r+t.r,i+t.i,);,CComplex,CComplex:,operator,-,(,CComplex,&t),return,CComplex(r-t.r,i-t.i,);,重载运算符的函数的定义形式,friend,返回类型,类名,:,operator,重载的运算符(参数列表),相关操作;,参数的个数由以下两个因素决定:,1,操作符是,单目,还是,双目运算符,2,定义为友元函数还是成员函数,友元函数,成员函数,单目运算符,1,个参数,0,个参数,双目运算符,2,个参数,1,个参数,在,C+,中提供了两种形式的运算符重载,即重载为:,成员函数、友元函数,如果是友元函数,那么对于,单目运算符,,它的参数个数就是,1,个,,双目运算符,的参数个数是,2,个;如果是成员函数,那么对于,单目运算符,的参数个数为,0,,,双目运算符,的参数个数是,1,个(,这是由于调用该成员函数的对象本身也作为一个操作数参与计算)。,一、重载为类的成员函数,类中的函数原型声明:,返回类型,operator,重载的运算符,(参数类型列表),;,程序中出现(以双目运算符为例),C1,运算符,C2,C1,.,operator,运算符,(C2),编译程序解释为函数调用:,其中,,C1,和,C2,是类的对象,,operator,运算符,则是,运算符重载函数名。,1,、双目运算重载为成员函数:,1,个参数,定义形式:,返回类型,类名,:,operator,重载的运算符,(参数列表),相关操作;,对表达式,a+b,、,ac,,编译器将其解释为函数调用:,a.operator,+(b);,a.operator,(c);,Complex,Complex:,operator,+,(Complex&t),return,Complex(r+t.r,i+t.i,);,Complex,Complex:,operator,-,(Complex&t),return,Complex(r-t.r,i-t.i,);,Complex,operator+,(Complex,Complex,operator-,(Complex,operator,运算符,是,重载运算符的函数名。,#include,class,CComplex,public:,CComplex(double,rt,=0,double it=0);,void,disp,();,void,set(double,double,);,void,get(double&,double,CComplex,operator+,(,CComplex,CComplex,operator-,(,CComplex,CComplex,operator+=,(,CComplex,private:,double,r,i,;,;,CComplex:CComplex(double,rt,double,it),r=,rt;i,=it;,void,CComplex:set(double,rt,double,it),r=,rt;i,=it;,void,CComplex:get(double,&,rt,double,&it),rt,=,r;it,=i;,void,CComplex:disp,(),if(,r|r,=0&i=0),cout,0&r),cout,+;,if(i),if(i=1);,else if(i=-1),cout,-;,else,cout,i;,cout,i;,cout,endl,;,CComplex,CComplex:,operator,+,(,CComplex,&t),return,CComplex(r+t.r,i+t.i,);,CComplex,CComplex:,operator,-,(,CComplex,&t),return,CComplex(r-t.r,i-t.i,);,CComplex,operator+=(,CComplex,&b),r+=,b.r,;i+=,b.i,;,return*this,;,void main(),CComplex,a(2,3),b(4,5);,CComplex,c(a+b,);,a.disp,();,b.disp,();,c.disp,();,c=a-b;,c.disp,();,a+=c;,a.disp,();,复数的运算符重载,(,成员函数,),2+3i,4+5i,6+8i,-2-2i,i,a.operator,+(b),a.operator,-(b),a.operator,+=(c),此时不再创建新对象,this,指针,是一个无需定义的特殊指针,它隐含于类的每一个,(,非静态,),成员函数中,指向调用该成员函数的当前对象。,如果某个对象调用了一个成员函数,则系统首先将这个对象的地址赋给,this,指针,因此可以用,*,this,来标识调用该成员函数的当前对象。,complex3,#include,#include,class string,public:,string(const,char*);,string(string,string();,int,strlength,();,void,disp,();,string operator=(string,string operator+(string,int,strcomp(string,int,operator(string,private:,char*,pstr,;,int,length;,;,string:string(const,char*sp),length=,strlen(sp,);,pstr,=new char length+1;,strcpy(pstr,sp,);,string:string(string,&sp),length=,sp.length,;,pstr,=new char length+1;,strcpy(pstr,sp.pstr,);,string:string,(),delete,pstr,;,int,string:strlength,(),return length;,void,string:disp,(),cout,pstr,(string&sp),if(,strcomp(sp,)0)return 1;,elsereturn 0;,void main(),string s1(1234567890);,string s2(abcdefg);,s1.disp();,s2.disp();,int,k=s1s2;,cout,ks1;,cout,k,endl,;,s1=s1+s2;,s1.disp();,s1=s2;,s1.disp();,s1.operator=(s1.operator+(s2),s1.operator=(s2),string1,注意,关于“”赋值运算:,C+,允许同类对象间的赋值,,将其对应的数据成员赋值;如果在构造函数中有资源申请,则不能使用系统的赋值方式,而必须重载赋值运算符,如,string,类;没有资源分配,可不重载赋值运算符,如,CComplex,类。,不能自造运算符:如字符串的比较函数,没有相应的运算符,不能用户自己定义。,返回类型 类名,:,operator,-(),相关操作;,2,、单目运算重载为成员函数:无参数,1,)负号运算符,#include,class,CComplex,public:,CComplex(double,rt,=0,double it=0);,void,disp,();,void,set(double,double,);,void,get(double&,double,CComplex,operator+,(,CComplex,CComplex,operator-,(,CComplex,CComplex,operator+=,(,CComplex,CComplex,operator-,();/,对单目负号重载,private:,double,r,i,;,;,CComplex,CComplex:,operator,-,()/,对单目负号重载,/,第一个,Complex,为函数返回值类型,第二个,Complex,为类名,return,CComplex(-r,-i,);,#include,class,CComplex,public:,CComplex(double,rt,=0,double it=0);,void,disp,();,void,set(double,double,);,void,get(double&,double,CComplex,operator+,(,CComplex,CComplex,operator-,(,CComplex,CComplex,operator+=,(,CComplex,CComplex,operator-,();/,对单目负号重载,private:,double,r,i,;,;,CComplex:CComplex(double,rt,double,it),r=,rt;i,=it;,void,CComplex:set(double,rt,double,it),r=,rt;i,=it;,void,CComplex:get(double,&,rt,double,&it),rt,=,r;it,=i;,void,CComplex:disp,(),if(,r|r,=0&i=0),cout,0&r),cout,+;,if(i),if(i=1);,else if(i=-1),cout,-;,else,cout,i;,cout,i;,cout,endl,;,CComplex,CComplex:,operator,+,(,CComplex,&t),return,CComplex(r+t.r,i+t.i,);,CComplex,CComplex:,operator,-,(,CComplex,&t),return,CComplex(r-t.r,i-t.i,);,CComplex,operator+=(,CComplex,&b),r+=,b.r,;i+=,b.i,;,return*this,;,CComplex,CComplex:,operator,-,(),return,CComplex(-r,-i,);,完整程序,void main(),CComplex,a(2,-3),b;,b=-a;,a.disp,();,b.disp,();,complex4,+,分:先增和后增,返回类型 类名,:,operator,+,(),相关操作;,先增:,后增:,返回类型 类名,:,operator,+,(,int,),相关操作;,int,只是为了标志前后的区别,没有其他作用。,2,、单目运算重载为成员函数,2,)自增,+,、自减,-,#include,class,CPoint,public:,CPoint(int,xx=0,int,yy,=0),x=xx;y=,yy,;,void,disp,(),cout,(x,y),endl,;,CPoint,operator+(,CPoint,q),return,CPoint(x+q.x,y+q.y,);,CPoint,operator-(,CPoint,q),return,CPoint(x-q.x,y-q.y,);,CPoint,operator+()/,先增,x+;y+;return*this;,CPoint,operator+(,int,)/,后增,CPoint,t(x,y,);x+;y+;return t;,CPoint,operator-(),x-;y-;return*this;,CPoint,operator-(,int,),CPoint,t(*this);x-;y-;return t;,private:,int,x,y,;,;,void main(),CPoint,a(10,10),b;,a.disp,();,b=a+;,a.disp,();,b.disp,();,b=+a;,a.disp,();,b.disp,();,b=a-;,a.disp,();,b.disp,();,b=-a;,a.disp,();,b.disp,();,(10,10),(11,11),(10,10),(12,12),(12,12),(11,11),(12,12),(10,10),(10,10),point2,this,指针的使用,*,this,3,、下标运算符,重载为成员函数:数组类,#include,class,CArray,public:,CArray(int,size);,CArray(CArray,CArray,();,int,getAt(int,nIndex,);,void,setAt(int,nIndex,int,newElement,);,int,&operator(,int,);,void operator=(,CArray,private:,int,*data;/,整型数组首地址,int,size;,/,数组中的元素个数,;,CArray:CArray(int,s),size=s;,data=new,intsize,;,CArray:CArray,(),delete data;,CArray:CArray(CArray,&a),size=,a.size,;,data=new,intsize,;,for(,int,i=0;i,size;i,+),datai,=,a.datai,;,int,CArray:getAt(int,nIndex,),return,datanIndex,;,void,CArray:setAt(int,nIndex,int,newElement,),datanIndex,=,newElement,;,int,&,CArray:operator,(,int,nIndex,),return,datanIndex,;,void,CArray:,operator,=,(,CArray,&a),delete data;,size=,a.size,;,data=new,intsize,;,for(,int,i=0;i,size;i,+),datai,=,a.datai,;,void main(),CArray,a(10);,for(,int,i=0;i10;i+),ai,=i*2+1;,for(i=0;i10;i+),cout,ai,t;,cout,endl,;,array1,#include,template,class,CArray,public:,CArray(int,size);,CArray(CArray,CArray,();,AB,getAt(int,nIndex,);,void,setAt(int,nIndex,AB,newElement,);,AB,&operator(,int,);,void operator=(,CArray,private:,AB,*data;/,整型数组首地址,int,size;,/,数组中的元素个数,;,template,CArray,:,CArray(int,s),size=s;,data=new,AB,size,;,template,CArray,:,CArray,(),delete data;,template,CArray,:,CArray(CArray,&a),size=,a.size,;,data=new,AB,size,;,for(,int,i=0;i,size;i,+),datai,=,a.datai,;,template,AB,CArray,:,getAt(int,nIndex,),return,datanIndex,;,template,void,CArray,:,setAt(int,nIndex,AB,newElement,),datanIndex,=,newElement,;,template,AB,&,CArray,:operator(,int,nIndex,),return,datanIndex,;,template,void,CArray,:,operator=,(,CArray,&a),delete data;,size=,a.size,;,data=new,AB,size,;,for(,int,i=0;i,size;i,+),datai,=,a.datai,;,void main(),int,n=10;,CArray,a(,n,);,CArray,b,(,n,);,for(,int,i=0;i,n,;i,+),ai,=i*2+1;,b,i,=i*2,.1,+1;,for(i=0;i,n,;i,+),cout,ai,t;,for(i=0;i,n,;i,+),cout,b,i,t;,array2,class string,public:,string(const,char*);,string(string,string();,void,disp,();,char&operator(,int,);,private:,char*,pstr,;,int,length;,;,char&,string:operator,(,int,i),return,pstri,;,字符串类:下标运算符,void main(),string s1(1234567890);,s1.disp();,cout,s15,输出,必须重载为友元,复数的双目运算符重载,(,友元函数,),2+3i,4+5i,6+8i,-2-2i,i,#include,class,CComplex,public:,CComplex,(double a=0,double b=0);,void,disp,();,friend,CComplex,operator+(,CComplex,&,CComplex,friend,CComplex,operator-(,CComplex,&,CComplex,friend,CComplex,operator+=(,CComplex,&,CComplex,private:,double,r,i,;,;,CComplex:CComplex,(double,a,double,b),r=,a,i,=b;,void,CComplex:disp,(),if(,r|r,=0&i=0),cout,0&r),cout,+;,if(i),if(i=1);,else if(i=-1),cout,-;,else,cout,i;,cout,i;,cout,endl,;,CComplex,operator+(,CComplex,&,a,CComplex,&b),return,CComplex(a.r+b.r,a.i+b.i,);,CComplex,operator-(,CComplex,&,a,CComplex,&b),return,CComplex(a.r-b.r,a.i-b.i,);,CComplex,operator+=(,CComplex,&,a,CComplex,&b),a.r,+=,b.r,;,a.i,+=,b.i,;return a;,void main(),CComplex,a(2,3),b(4,5);,CComplex,c(a+b,);,a.disp,();,b.disp,();,c.disp,();,c=a-b;,c.disp,();,a+=c;,a.disp,();,operator-(,a,b,),operator+=(,a,c,),operator+(,a,b,),complex5,2,、复数的单目运算符重载,(,友元函数,),void main(),CComplex,a(10,10),b;,a.disp,();,b=a+;,a.disp,();,b.disp,();,b=+a;,a.disp,();,b.disp,();,b=a-;,a.disp,();,b.disp,();,b=-a;,a.disp,();,b.disp,();,10+10i,11+11i,10+10i,12+12i,12+12i,11+11i,12+12i,10+10i,10+10i,#include,class,CComplex,public:,CComplex,(double a=0,double b=0);,void,disp,();,friend,CComplex,operator+(,CComplex,&,CComplex,friend,CComplex,operator-(,CComplex,&,CComplex,friend,CComplex,operator+=(,CComplex,&,CComplex,friend,CComplex,operator+(,CComplex,friend,CComplex,operator+(,CComplex,&,a,int,);,friend,CComplex,operator-(,CComplex,friend,CComplex,operator-(,CComplex,&,a,int,);,private:,double,r,i,;,;,CComplex:CComplex,(double,a,double,b),r=,a,i,=b;,void,CComplex:disp,(),if(,r|r,=0&i=0),cout,0&r),cout,+;,if(i),if(i=1);,else if(i=-1),cout,-;,else,cout,i;,cout,i;,cout,endl,;,CComplex,operator+(,CComplex,&,a,CComplex,&b),return,CComplex(a.r+b.r,a.i+b.i,);,CComplex,operator-(,CComplex,&,a,CComplex,&b),return,CComplex(a.r-b.r,a.i-b.i,);,CComplex,operator+=(,CComplex,&,a,CComplex,&b),a.r,+=,b.r,;,a.i,+=,b.i,;return a;,CComplex,operator+(,CComplex,&a),a.r,+;,a.i,+;return a;,CComplex,operator+(,CComplex,&,a,int,),CComplex,t(a.r+,a.i,+);return t;,CComplex,operator-(,CComplex,&a),a.r,-;,a.i,-;return a;,CComplex,operator-(,CComplex,&,a,int,),CComplex,t(a.r-,a.i,-);return t;,complex6,运算符重载的原则,运算符重载,不改变运算符的优先级和结合性,,不改变其语法结构,也就是,不能改变操作数的个数,。即单目的只能重载为单目运算符,双目的只能重载为双目运算符。,只能对已有运算符重载,不能自定义运算符,。,可重载的运算符,运算符,名,称,运,算 符,名,称,,,逗号运算符,小于,!,=,不等,左移,%,取模,=,左移,/,赋值,%=,取模,/,赋值,大于,*,乘,=,大于等于,*,=,乘,/,赋值,右移,+,加,=,右移,/,赋值,+=,加,/,赋值,异或,-,减,=,异或,/,赋值,-=,减,/,赋值,|,按位或,-,成员选取,|=,按位或,/,赋值,/,除,|,逻辑或,/=,除,/,赋值,不可重载的,5,个运算符:,.*:?:,sizeof,8-1.,定义复数类,实现复数的,+,、,-,、,+,、先,+,、后,+,、先,-,、后,-,运算符重载,使用,成员函数,和友元函数分别实现,(,两个程序,),。,8-2.,定义点类,并实现点的,+,、,-,、,+,的运算符重载:分别使用成员函数和友元函数实现,(,两个程序,),。,8-3.,定义 整型数组类,使用成员函数实现下标,运算符的重载。,8-4.,定义时间类,进行时间的自增、自减运算符重载。,8-5.*,定义字符串类,实现字符串的,+,、,=,、,=,、,等运算符的重载。,8-6.*,实现上述类型的输入输出重载(用友元函数实现),作业8,#include,class time,private:,int,hour,minute,second,;,public:,time(int,x=0,int y=0,int z=0),hour=,x,minute,=,y,second,=y;,void,showTime,(),cout,hour:minute,:second=60),second-=60;,minute+;,if(minute,=60),minute-=60;,hour+;,hour%=24;,/,cout,=60),second-=60;,minute+;,if(minute,=60),minute-=60;,hour+;,hour%=24;,/,cout,time+n;,void main(),time mytime(23,59,59);,cout,First Time output:;,mytime.showTime,();,mytime,+;,mytime.showTime,();,+,mytime,;,mytime.showTime,();,8-4.*,定义时钟类,进行时间的自增、自减运算符重载。,展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




chap6_运算符重载.ppt



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/13221173.html