您好,欢迎访问三七文档
11.声明一个Circle类,有1)数据成员Radius(半径)2)成员函数GetValue()用来给半径赋值3)成员函数GetArea()计算圆的面积在主函数中创建一个Circle类的对象进行测试,显示出面积。#includeiostreamusingnamespacestd;classcircle{private:intradius;public:voidgetvalue(){cout输入半径:;cinradius;}doublegetarea(){return3.14*radius*radius;}voidoutput(){cout半径是:radius,面积是:getarea();}};intmain(){circlecircle1;doubleradius;circle1.getvalue();circle1.getarea();circle1.output();}输入半径:2半径是:2,面积是:12.562.声明一个tree类,有1)数据成员ages(树龄)2)成员函数grow(intyears)对ages加上years3)成员函数age()显示对象的数据成员ages的值4)构造函数tree(intn=0)进行初始化在主函数中创建一个tree类的对象进行测试(创建一个树龄为18的对象,调用age()显示树龄,之后调用grow(4),生长4年,再显示树龄)。2:#includeiostreamusingnamespacestd;classtree{public:tree(inta){ages=a;}voidgrow(intyears);voidage();private:intages;};voidtree::grow(intyears){cout请输入年份:;cinyears;ages+=years;}voidtree::age(){cout该树年龄为:agesendl;}intmain(){treet1(5);t1.age();t1.grow(4);t1.age();system(pause);return0;}该树年龄为:5请输入年份:5该树年龄为:103.声明一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两点的坐标,并有成员函数计算矩形的周长及面积。编程实现求左下角与右上角坐标分别为(2.1,3.2),(5.2,6.3)的矩形周长及面积。#includeiostream.h#includemath.hclassRectangle{private:doubleleft;doublebottom;doubleright;doubletop;public:voidSetPoint(doublel,doubleb,doubler,doublet){left=l;bottom=b;right=r;top=t;}voidcal(doubleleft,doublebottom,doubleright,doubletop){doubles;s=abs(left-right)*abs(bottom-top);cout该长方形的面积为:sendl;}voidzhou(doubleleft,doublebottom,doubleright,doubletop){doublez;z=2*(abs(left-right)+abs(bottom-top));cout该长方形的周长为:2zendl;}voidGetp1(doubleleft,doublebottom){cout长方形的左下角坐标是:left,bottomendl;}voidGetp2(doubleright,doubletop){cout长方形的右上角坐标是:right,topendl;}};voidmain(){Rectangler1;doublel,b,r,t;cout请输入长方形的左下角的坐标:;cinlb;cout请输入长方形的右上角的坐标:;cinrt;r1.Getp1(l,b);r1.Getp2(r,t);r1.cal(l,b,r,t);r1.zhou(l,b,r,t);}请输入长方形的左下角的坐标:2.13.2请输入长方形的右上角的坐标:5.26.3长方形的左下角的坐标:2.13.2长方形的右上角的坐标:5.26.31.编写一个简单复数类Scomplex,要求用友元函数重载“+”、“-”运算符,用成员函数重载“=”运算符,使之能够实现整数或浮点数和复数的加法和减法,并且进行测试。1.#includeiostreamusingnamespacestd;classScomplex{private:doublereal;doubleimage;public:Scomplex(doublex=0,doubley=0);friendScomplexoperator+(inta,Scomplexb);friendScomplexoperator-(inta,Scomplexb);friendScomplexoperator+(doublea,Scomplexb);friendScomplexoperator-(doublea,Scomplexb);Scomplex&operator=(constScomplex&b);voidprint();};Scomplex::Scomplex(doublex,doubley){real=x;image=y;}Scomplexoperator+(inta,constScomplexb){Scomplextemp;temp.real=a+b.real;temp.image=b.image;returntemp;}Scomplexoperator+(doublea,constScomplexb){Scomplextemp;temp.real=a+b.real;temp.image=b.image;returntemp;}Scomplexoperator-(doublea,constScomplexb){Scomplextemp;temp.real=a-b.real;temp.image=-b.image;returntemp;}Scomplexoperator-(inta,constScomplexb){Scomplextemp;temp.real=a-b.real;temp.image=-b.image;returntemp;}Scomplex&Scomplex::operator=(constScomplex&b){if(this==&b){return*this;}real=b.real;image=b.image;return*this;}voidScomplex::print(){coutreal;3if(image=0){cout'+';}coutimage'i'endl;}intmain(){Scomplext1(1,2),t2(3,4),t3;t3=1+t2;t3.print();t3=2-t2;t3.print();t3=t1;t3.print();return0;}4+4i-1-4i1+2i2.空间一点p的坐标为(x,y,z),其中x,y,z为整数。编写点类Point3D,定义空间两点之间的加”+”,减”-”运算为相应三个坐标值分别进行加、减运算,要求实现空间两点之间的加”+”减”-”赋值”=”运算,空间两点间的比较”==”运算。要求编写Point3D类的声明定义和测试程序。#includeiostreamusingnamespacestd;classPoint3D{private:intx,y,z;public:Point3D(inta=0,intb=0,intc=0);Point3Doperator+(Point3Db);Point3Doperator-(Point3Db);Point3D&operator=(constPoint3D&b);booloperator==(Point3Db);voidprint();};Point3D::Point3D(inta,intb,intc){x=a;y=b;z=c;}Point3DPoint3D::operator+(Point3Db){Point3Dtemp;temp.x=x+b.x;temp.y=y+b.y;temp.z=z+b.z;returntemp;}Point3DPoint3D::operator-(Point3Db){Point3Dtemp;temp.x=x-b.x;temp.y=y-b.y;temp.z=z-b.z;returntemp;}Point3D&Point3D::operator=(constPoint3D&b){if(this==&b){return*this;}x=b.x;y=b.y;z=b.z;return*this;}boolPoint3D::operator==(Point3Db){intt1,t2,t3;t1=x-b.x;t2=y-b.y;t3=z-b.z;if(t1==0&&t2==0&&t3==0)returntrue;elsereturnfalse;}voidPoint3D::print(){cout(x,y,z)endl;}intmain(){Point3Dp1(1,1,1),p2(2,2,2),p3;4p3=p2+p1;p3.print();p3=p2-p1;p3.print();p3=p1;p3.print();if(p1==p2){coutp1=p2endl;}else{coutp1!=p2endl;}return0;}(3,3,3)(1,1,1)(1,1,1)p1!=p23.设计一个时间类Time,包括时、分、秒等私有数据成员。重载“+”和“-”运算符以实现时间的加法和减法运算,并进行测试。#includeiostreamusingnamespacestd;classTime{private:inthour,minute,second;public:Time(inta=0,intb=0,intc=0);Timeoperator+(Timex);Timeoperator-(Timex);voidprint();~Time(){}};Time::Time(inta,intb,intc){hour=a;minute=b;second=c;}TimeTime::operator+(Timex){Timetemp;temp.second=second+x.second;temp.minute=minute+x.minute;temp.hour=hour+x.hour;if(temp.second59){temp.minute+=1;temp.second-=60;}if(temp.minute59){temp.hour+=1;temp.minute-=60;}if(temp.hour23){temp.hour-=24;}returntemp;}TimeTime::operator-(Timex){Timetemp;temp.second=second-x.second;temp.minute=minute-x.minute;temp.hour=hour-x.hour;if(temp.second0){temp.minute-=1;temp.second+=60;}if(temp.minute0){temp.hour-=1;temp.minute+=60;}if(temp.hour0){temp.hour+=24;}returntemp;}5voidTime::print(){couthour:minute:secondendl;}
本文标题:c++实验答案
链接地址:https://www.777doc.com/doc-4285607 .html