您好,欢迎访问三七文档
1实验7运算符重载(*)一、实验目的掌握C++中运算符重载的机制和运算符重载的方式;二、实验内容及步骤1.编写一个简单复数类Scomplex,要求用友元函数重载“+”、“-”运算符,用成员函数重载“=”运算符,使之能够实现整数或浮点数和复数的加法和减法,并且进行测试。、#includeiostream#includecmath#includeiomanip#includecstdlibusingnamespacestd;classScomplex{private:floatreal;floatimage;public:Scomplex(){real=0;image=0;}Scomplex(floatr,floati){real=r;image=i;}voidShow(){coutfixedsetprecision(2)(real,image)endl;}friendScomplexoperator+(Scomplexcomp,intiNum);friendScomplexoperator-(Scomplexcomp,intiNum);friendScomplexoperator+(Scomplexcomp,floatfNum);friendScomplexoperator-(Scomplexcomp,floatfNum);Scomplexoperator=(Scomplexnum);};ScomplexScomplex::operator=(Scomplexnum){2this-real=num.real;this-image=num.image;return*this;}Scomplexoperator+(Scomplexcomp,intiNum){comp.real=comp.real+(float)iNum;returncomp;}Scomplexoperator+(intiNum,Scomplexcomp){comp=comp+iNum;returncomp;}Scomplexoperator-(Scomplexcomp,intiNum){comp.real=comp.real-(float)iNum;returncomp;}Scomplexoperator-(intiNum,Scomplexcomp){comp=comp-iNum;returncomp;}Scomplexoperator+(Scomplexcomp,floatfNum){comp.real=comp.real+fNum;returncomp;}Scomplexoperator+(floatfNum,Scomplexcomp){//comp.real=comp.real+fNum;comp=comp+fNum;returncomp;}Scomplexoperator-(Scomplexcomp,floatfNum){3comp.real=comp.real-fNum;returncomp;}Scomplexoperator-(floatfNum,Scomplexcomp){comp=comp-fNum;returncomp;}intmain(void){intiData=10;floatfData=66.6;Scomplexcomp(23.5,86.5);ScomplexIntAdd,IntSub,FloatAdd,FloatSub,AssigNum;IntAdd=iData+comp;IntSub=iData-comp;FloatAdd=fData+comp;FloatSub=fData-comp;AssigNum=comp;cout整数加上复数:;IntAdd.Show();cout整数减上复数:;IntSub.Show();cout浮点数加上复数:;FloatAdd.Show();cout浮点数减上复数:;FloatSub.Show();cout重载赋值运算符后:;AssigNum.Show();system(PAUSE);return0;42.空间一点p的坐标为(x,y,z),其中x,y,z为整数。编写点类Point3D,定义空间两点之间的加”+”,减”-”运算为相应三个坐标值分别进行加、减运算,要求实现空间两点之间的加”+”减”-”赋值”=”运算,空间两点间的比较”==”运算。要求编写Point3D类的声明定义和测试程序。头文件Poin3D.h#ifndefPOINT3D_H#definePOINT3D_H#includeiostreamusingnamespacestd;classPoint3D{private:intx,y,z;public:Point3D(){x=0;y=0;z=0;}Point3D(intxx,intyy,intzz):x(xx),y(yy),z(zz){}voidPrint()const{coutx,y,zendl;}Point3Doperator+(constPoint3D&pnt)const;Point3Doperator-(constPoint3D&pnt)const;Point3Doperator=(constPoint3D&pnt);booloperator==(constPoint3D&pnt)const;};Point3DPoint3D::operator+(constPoint3D&pnt)const5{Point3Dtemp;temp.x=this-x+pnt.x;temp.y=this-y+pnt.y;temp.z=this-z+pnt.z;returntemp;}Point3DPoint3D::operator-(constPoint3D&pnt)const{Point3Dtemp;temp.x=this-x-pnt.x;temp.y=this-y-pnt.y;temp.z=this-z-pnt.z;returntemp;}Point3DPoint3D::operator=(constPoint3D&pnt){this-x=pnt.x;this-y=pnt.y;this-z=pnt.z;return*this;}boolPoint3D::operator==(constPoint3D&pnt)const{if((this-x==pnt.x)&&(this-y==pnt.y)&&(this-z==pnt.z))return1;elsereturn0;}#endif源文件:Point3D.cpp#includePoint3D.hintmain(void){Point3Dp1(7,8,9);6Point3Dp2(1,2,3);Point3DPointAdd,PointSub,PointAssign;Point3DRightComp(1,2,3);Point3DWrongComp(4,5,6);PointAdd=p1+p2;PointSub=p1-p2;PointAssign=p1;cout两点坐标的和:;PointAdd.Print();cout两点坐标的差:;PointSub.Print();cout两点坐标进行赋值:;PointAssign.Print();if(RightComp==p2)cout两点坐标值一样.endl;elsecout两点坐标值不一样.endl;if(WrongComp==p2)cout两点坐标值一样.endl;elsecout两点坐标值不一样.endl;return0;}73.设计一个时间类Time,包括时、分、秒等私有数据成员。重载“+”和“-”运算符以实现时间的加法和减法运算,并进行测试。#includeiostream#includecstdlibusingnamespacestd;classTime{private:inthour,minute,second;public:Time(){hour=0;minute=0;second=0;}Time(inth,intm,ints){hour=h;minute=m;second=s;}voidPrint();friendTimeoperator+(Timet1,Timet2);friendTimeoperator-(Timet1,Timet2);};voidTime::Print(){couthour:minute:secondendl;}Timeoperator+(Timet1,Timet2){Timetemp;temp.hour=t1.hour+t2.hour;temp.minute=t1.minute+t2.minute;temp.second=t1.second+t2.second;temp.minute=temp.minute+temp.second/60;temp.second%=60;temp.hour=temp.hour+temp.minute/60;temp.minute%=60;temp.hour%=24;returntemp;}Timeoperator-(Timet1,Timet2)8{Timetemp;if(t1.second-t2.second0){t1.minute--;temp.second=t1.second+60-t2.second;}elsetemp.second=t1.second-t2.second;if(t1.minute-t2.minute0){t1.hour--;temp.minute=t1.minute+60-t2.minute;}elsetemp.minute=t1.minute-t2.minute;if(t1.hour-t2.hour0)temp.hour=24+t1.hour-t2.hour;elsetemp.hour=t1.hour-t2.hour;returntemp;}intmain(void){Timet1(11,42,55),t2(1,1,1);TimeTimeAdd,TimeSub;TimeAdd=t1+t2;TimeSub=t1-t2;cout时间相加结果:;TimeAdd.Print();cout时间相减结果:;TimeSub.Print();system(PAUSE);return910
本文标题:运算符重载实验
链接地址:https://www.777doc.com/doc-4597172 .html