运算符重载几种常用_第1页
运算符重载几种常用_第2页
运算符重载几种常用_第3页
运算符重载几种常用_第4页
运算符重载几种常用_第5页
免费预览已结束,剩余35页可下载查看

付费下载

下载本文档

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

文档简介

14讲运算符重载本次议 符 new 一在C语言中,操作符表示将整数向右移位,操作符<<表示将整数向左移位: 000010000000001000000100x0000001000000100intintoperator>>(int,int);intoperator<<(int,int);在C++中,系统重载了操作符>>-输入操作

short&istream&istream::operator>>(int&

istream::operator>>(long&_Val);一在C++中,系统重载了操作符-输入操作。 >>iv>>cin.operator>>(

.operator>>(dvistream&istream&istream&is,string&ss1,cin>>cin>>s1>>重载为?string::operator>>iss1,s1>>在C++中,系统重载了操作符-输入操作。 XXc1,c1,>>c1>> istream&operator>>(istream& Complex&c流非in>>c.real>>非 }////ostream&oerator<<T{out<<out<<return}{{…一class{friendistream&operator>>(istream&in,Complexfriendostream&operator<<(ostream&out,constComplexistream&operator>>(istream&in,Complex{in>>in>>return}ostream&operator<<(ostreamostream&operator<<(ostream&out,constComplex{out<<out<<showpos<<arg.m_dImag<<returnout;}void{Complexcin>>c1;//operator>>(cin,cin>>cout<<c1<<c2<< 小 ostream&operator<<(ostream&out,constComplex{out<<arg.m_dReal<<showpos<<arg.m_dImag<<"i";return}cout<<obj<<obj1;//operator<<(cout, cin>>obj1>>cout<<obj1<<obj2;、必须以非静态成员函数自定义的类如果没有重载赋值运算符,系统会自动重classComplexdoublem_dReal,

c2=

c1(10),//

//c2.real//c2.imag

、ComplexComplex&operator=(constComplexComplex&Complex::operator=(constComplexif(this&ref)//防止自{m_dReal=m_dImag=}return 二、重载赋值运算符 CString&CString::operator=(constCString{if(this!={delete[]_m_iSize=m_p=new}}strcpy(m_p,}returnCstringCstrings2 Cstrings3s1;//二、重载赋值运算符 派生类要显式调用直接基类的DEMO 只能用非静态成员函TYPETYPE&operator[](constTint&DynamicArray::operator[](constint{if(aIndex>m_iLocation||aIndex<0{}return}structStudent{char*name; //constintMAX_TABLE_SIZE=classStudentTableStudentStudent&operator[](constchar*);Student&operator[](constint);//

StudentTablest;voidf(){Students1=st[" Students2=st[ //}Student&StudentTable::operator[](constchar*{for(inti=0;i<MAX_TABLE_SIZE;i++)if(strcmp(p,body[i].name)==0)return}Student&StudentTable::operator[](constint{for(inti=0;i<MAX_TABLE_SIZE;i++)if(n==body[i].studentNo)return}三、重载下标运算符“下标运算符“”的重载一定返回对象的,目的是作为左值;constT&T::operator[](constType&const//用于常对 TYPE&operator[](constT 、void*类名::operatornew(size_t, 只能作为静态 只能作为静态成员函 newTresultsinacallofoperatornew(2,f)Tresultsinacallofoperator,这个参数的值是sizeof(T),这个参数由系统赋四、重载new和void类名::operatorvoid类名::operatordelete(void 形参表中 形参表中至少含一个void*参 若有第二个参数,第二个参数必须为DEMO、 调用方式:显示调用或者隐式调DEMOComplexdoublet2+c2c2转换成double类型doubletmp;cout<<"c1="<<c1<<endl;tmp=double(c1);//显示调用couttmptmp<<endl; Demo同时出现“二义性”!Demo10class{,

void{Complex24 operatordouble();intint

、 形参表中至少有一个 只能用成员函数实现重DEMO本次议 符 new 不同类型数据间的转换classclassComplex(constdoubleaReal,constdoubleaImag);doublem_dReal;doubleclassclassCoordinate(constdouble,constoperatorComplex()doubledouble不同类型数据间的转换Coordinate::Coordinate::operatorComplex(){returnComplex(m_dX,}int{Coordinatecoo(3,Complexcom(0,com=cout<<com<<return}

不同类型数据间的转换不同类型数据间的转换classCoordinate(constdouble,constdouble);friendComplex;doublem_dX,classComplex{Complex(constdouble,constComplex(constCoordinateComlex&Comlex::oerator=constComplexfriendostream&operator<<(ostream&,constComplex&);doublem_dRealplex(constCoordinate{m_dReal=m_dImag=}Complex&Complex::operator=(constComplex{if(this!={m_dReal=aC.m_dX;m_dImag=}return}int{

Coordinatecoo(3,Complexcom(0,com=}不同类型数据间的转换不同类型数据间的转换classCoordinate(constdouble,constdoublefrienddoublem_dX,classComplex(constdouble,constComplex&Complex::operator=(constCoordinatedoublem_dReal,{m_dReal=aC.m_dX;m_dImag=aC.m_dY;return*this;}intint{Coordinatecoo(3,Complexcom(0,com=cout<<com<<return}面讲的例子中,我们可以看到五花八门的参数对于函数的形参,如果仅需要而不是改变它,那const来传递它(使用const对象也可以,但会引起对象的,浪费时间和空间),如果是成员函friendfriendconstComplexoperator+(constComplex&,constComplex&//constComplexoperator+(constComplex&)const//成员函数重载+friendconstComplexoperator-(constComplex&,constComplex&);//减法friendconstComplexoperator*(constComplex&,constComplex&);//乘 Complex& sC

温馨提示

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

评论

0/150

提交评论