chap6运算符重载

上传人:lx****y 文档编号:243030716 上传时间:2024-09-14 格式:PPT 页数:28 大小:110.50KB
返回 下载 相关 举报
chap6运算符重载_第1页
第1页 / 共28页
chap6运算符重载_第2页
第2页 / 共28页
chap6运算符重载_第3页
第3页 / 共28页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,算术运算符:加(,+,)、减,string s1,s2;/s1,s2,为字符串对象,s1+s2/?,Complex a,b;/,复数类对象,a+b/?,a-b/?,x+y/x,y,为,int,float,double,char.,问题引出:,运算符重载,1,问题引出:自定义类型的运算,#include ,class Complex,public:,Complex(double rt=0,double it=0) r=rt;i=it;,void disp();,Complex add(Complex ,Complex dec(Complex ,private:,double r,i;,;,void Complex:disp(),if (i0) coutr+iiendl;,else if (i0) coutriiendl;,else coutr0) coutr+iiendl;,else if (i0) coutriiendl;,else coutrendl;,Complex Complex:,operator +,(Complex &t),double rt=r+t.r;,double it=i+t.i;,return Complex(rt,it);,Complex Complex:,operator -,(Complex &t),return Complex(r-t.r,i-t.i);,void main(),Complex 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 ,3,#include ,class CPoint,public:,CPoint(int xx=0,int yy=0),x=xx;y=yy;,void disp(),cout(x,y),”,既作为流的插入运算符,又作为移位运算符,根据其操作对象的不同,执行其不同的功能。,在,c+,中,允许对大多数的运算符进行重载,通过,定义重载运算符的函数,使它能够用于特定类的对象。,5,运算符重载的实现,运算符重载实际上是将运算符重载为一个函数,在重载某个运算符时,实际上就是定义一个,重载运算符的函数,,函数名为,operator,运算符,。在执行被重载的运算符时,系统自动调用该函数以实现相应的运算,对表达式,a+b,、,ab,,编译器将其解释为函数调用:,a.operator +(b);,a.operator (b);,运算符重载是通过定义函数实现的,运算符重载实质上是函数的重载(遵循函数重载的原则)。,对于重载的运算符可以使用运算符方式调用,也可以使用函数调用方式调用。,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);,6,重载运算符的函数的定义形式,friend,返回类型,类名,:,operator,重载的运算符(参数列表),相关操作;,参数的个数由以下两个因素决定:,1,操作符是,单目,还是,双目运算符,2,定义为友元函数还是成员函数,在,C+,中提供了两种形式的运算符重载,即重载为:,成员函数、友元函数,如果是友元函数,那么对于,单目运算符,,它的参数个数就是,1,个,,双目运算符,的参数个数是,2,个;如果是成员函数,那么对于,单目运算符,的参数个数为,0,,,双目运算符,的参数个数是,1,个(,这是由于该类本身也作为一个操作数参与计算)。,7,一、重载为类的成员函数,类中的函数原型声明:,返回类型,operator,重载的运算符(参数类型列表),;,程序中出现(以双目运算符为例),C1,运算符,C2,C1,.,operator,运算符,(C2),编译程序解释为函数调用:,其中,,C1,和,C2,是类的对象,,operator,运算符,则是,运算符重载函数名。,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 ,8,#include ,class Complex,public:,Complex (double a=0,double b=0),r=a,i=b;,void disp(),if (i0)cout r+iin;,else if (i0)coutriin;,elsecoutr(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 (),coutpstr (string &sp),if (strcomp(sp)0),return 1;,else,return 0;,void main(),string s1(1234567890),s2(abcdefg);,s1.disp();,s2.disp ();,int k=s1s2;,coutks1;,coutkendl;,s1=s1+s2;,s1.disp ();,s1=s2;,s1.disp();,s1.operator=(s1.operator +(s2),s1. operator =(s2),11,注意,关于“”赋值运算:,C+,允许同类对象间的赋值,,将其对应的数据成员赋值;如果在构造函数中有资源申请,则不能使用系统的赋值方式,而必须重载赋值运算符,如,string,类;没有资源分配,可不重载赋值运算符,如,Complex,类。,不能自造运算符:如字符串的比较函数,没有相应的运算符,不能用户自己定义。,12,class Complex,public:,Complex(double r=0,double i=0);,void Show();,Complex operator - ();/,对单目负号重载,private:,double r,i;,;,Complex Complex:operator - ()/,对单目负号重载,/,第一个,Complex,为函数返回值类型,第二个,Complex,为类名,return Complex(-r,-i);,返回类型 类名,:,operator,-(),相关操作;,2,、单目运算重载为成员函数,1,)负号运算符,13,+,分:先增和后增,返回类型 类名,:,operator,+,(),相关操作;,先增:,后增:,返回类型 类名,:,operator,+,(,int,),相关操作;,int,只是为了标志前后的区别,没有其他作用。,2,、单目运算重载为成员函数,2,)自增,+,、自减,-,14,#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),15,this,指针,是一个无需定义的特殊指针,它隐含于每一个类的成员函数中,指向正在被某个成员函数操作的对象。,如果某个对象调用了一个成员函数,则系统首先将这个对象的地址赋给,this,指针,然后再调用成员函数,因此也可以用,*,this,来标识调用该成员函数的对象。通常情况下,程序不显式地使用,this,指针。,16,2,、单目运算重载为成员函数,3,)下标运算符,#include ,class CArray,public:,CArray(int size);,CArray(CArray,CArray();,int,int getAt(int nIndex);,void setAt(int nIndex, int newElement);,private:,int* data; /整型数组首地址,int size; /数组中的元素个数,;,CArray:CArray(int s),size=s;,data=new intsize;,CArray:CArray(CArray &a),size = a.size;,data = new intsize;,for (int i=0;isize;i+),datai=a.datai;,CArray:CArray(),delete data;,int &CArray:operator (int nIndex),return datanIndex;,int CArray:getAt(int nIndex),return datanIndex;,void CArray:setAt(int nIndex, int newElement),datanIndex=newElement;,void main(),CArray a(10);,for (int i=0;i10;i+),ai=i*2+1;,for (i=0;i10;i+),coutait;,coutendl;,17,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();,couts15,输出,0)cout r+iin;,else if (i0)coutriin;,elsecoutr0)cout r+iin;,else if (i0)coutriin;,elsecoutrendl;,friend Complex operator +(Complex ,friend Complex operator +(Complex ,friend Complex operator -(Complex ,friend Complex operator -(Complex ,private:,double r,i;,;,Complex operator +(Complex &a),a.r+;a.i+;return a;,Complex operator +(Complex &a, int),Complex t(a.r+,a.i+);return t;,Complex operator -(Complex &a),a.r-;a.i-;return a;,Complex operator -(Complex &a,int),Complex t(a.r-,a.i-);return t;,Complex 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,21,当运算符重载函数为:,成员函数,-,:,双目运算符的参数个数是,1,个;,单目运算符的参数个数为,0,友元函数,-,单目运算符的参数个数就是,1,个,,双目运算符的参数个数是,2,个,这是因为重载为成员函数时,总是隐含了一个参数,该参数是,this,指针,,它是指向调用该成员函数对象的指针,而重载为友元函数时则没有隐含的,this,指针。,22,运算符重载的原则,运算符重载,不改变运算符的优先级和结合性,,不改变其语法结构,也就是,不能改变操作数的个数,。即单目的只能重载为单目运算符,双目的只能重载为双目运算符。,只能对已有运算符重载,不能自定义运算符,。,23,可重载的运算符,运算符,名 称,运 算 符,名 称,,,逗号运算符,小于,!,=,不等,左移,%,取模,=,左移,/,赋值,%=,取模,/,赋值,大于,*,乘,=,大于等于,*=,乘,/,赋值,右移,+,加,=,右移,/,赋值,+=,加,/,赋值,异或,-,减,=,异或,/,赋值,-=,减,/,赋值,|,按位或,-,成员选取,|=,按位或,/,赋值,/,除,|,逻辑或,/=,除,/,赋值,按位与,不可重载的运算符,5,个:,. .* : ?: sizeof,24,作业,1,、定义复数类,实现复数的,+,、,-,、,+,、,+,先、,+,后、,-,先、,-,后运算符重载:,成员函数,和友元函数。,2,、定义点类,并实现点的,+,、,-,、,+,的运算符重载:成员函数和友元函数。,3,、定义 整型数组类,实现下标,运算符的重载。,4,、定义时钟类,进行时间的自增、自减运算符重载。,5,、*定义字符串类,实现字符串的,+,、,=,、,=,、,等运算符的重载。,6,、*实现上述类型的输入输出重载(用友元函数实现),25,#include,class Clock,private :,int hour,minute,second;,public:,Clock(int x=0,int y=0,int z=0),hour=x,minute=y,second=y;,void showTime(),couthour: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;,coutClock+:;,27,void main(),Clock myClock(23,59,59);,coutFirst Time output:;,myClock.showTime ();,myClock+;,myClock.showTime ();,+myClock;,myClock.showTime ();,28,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 大学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!