《c++上机考试练习题.docx》由会员分享,可在线阅读,更多相关《c++上机考试练习题.docx(9页珍藏版)》请在优知文库上搜索。
1、第16周面向对向上机实验练习题类和对象1.已知一个名为ComPleX的复数类,这个类包含:(1)私有成员:实部、虚部,且均为Et型(2)公有的带默认形参值的构造函数、复制构造函数(3)公有成员函数DiSPlay,其作用为显示复数要求:(1)实现满足上述属性和行为的COmPleX类定义;(2)设计函数AddComplex,函数AddComplex功能为实现两个复数相加,要求该函数的形参为复数类的常引用;(3)保证如下主函数能正确运行,intmain()ComplexcO(2,-3);Complexcl(c);coutclis:;cl.Display();Complexc2(3,4);coutc2
2、is:;c2.Display();Complexc3;c3=AddCompIex(c1,c2);cout,c3is:,;c3.Display();return0;)且输出结果如下:clis:2-3ic2is:3+4ic3is:5+Ii2.已知表示点的类CPoint和表示线段的C1.ine类,类CPoiIlt包含:(D表达点位置的私有数据成员x,y(2)构造函数及复制构造函数类C1.ine包含:(1)两个CPoEt的点对象(该两点分别为线段的两个端点)(2)构造函数(提示:构造函数中用初始化列表对内嵌对象进行初始化)3)公有成员函数Get1.Ci,其功能为返回线段的长度(4)类属性成员CoImt
3、用于记录创建的C1.iIIe类对象的个数,及用于显示count值的ShowCount函数;要求:(1)实现满足上述属性和行为的CPoint类及C1.ine类定义;(2)保证如下主函数能正确运行,#includeusingnamespacestd;intmain()CPointpl(IJ);CPointp2(4,5);C1.inelinel(pl,p2);cout11thelengthofIinelis:11linel.Get1.en()endl;CPointp3(0,0);C1.ineline2(linel);cout11Thecountoflineis:C1.ine:ShowCount()e
4、ndl;returnO;)且输出结果如下:ThelengthofIinelis:5Thecountof1.ineis:2继承、派生3.已知Point类的声明如下:#includeusingnamespacestd;/Point类的声明classPoint(public:外部接口Point();构造函数Point(intxx,intyy);构造函数Point(Point&p);/潸贝构造函数intGetX()constreturnX;intGetY()constreturnY;voidmove(intncwx,intncwy)x=ncwx;y=newy;)-Point()private:私有数据
5、intX,Y;);classArrayofPointsPoint&element(intindex)assert(index=O&indexsize);/size为数组大小returnpointsindcx;计算任意两点之间的距离int1.lDistance(intxl,intyl,intx2,inty2)return(abs(xl-x2)+abs(yl-y2);)请给出POint类的构造函数,并参照下图创建动态数组类AlTayOfPOints,实现主函数。请自行补充需要的头文件。主程序intmain()intcount;cout,PleaseenterthecountofPoints,cou
6、nt;ArrayofPointsArrPoints(Count);ArrPoints.element(0).move(5,10);ArrPoints.element(l).move(15,20);cout,Thedistanceis”VVelld1;cout1.lDistance(ArrPoints.element(0).GetX(),ArrPoints.element(0).GetY(),ArrPoints.element(l).GetX(),ArrPoints.element(l).GetY()endl;)参考程序:#include#include#inc1udeusingnamespac
7、estd;classPoint(public:外部接口Point();构造函数Point(intxx,intyy);/构造函数Point(Point&p);/拷贝构造函数intGetX()const(returnx;)intGetY()const(returny;voidmove(intnewx,intnewy)x=newx;y=newy;)-Point()private:私有数据intx,y;1;Point:Point()x=O;y=O;)Point:Point(intxx,intyy)this-x=xx;this-y=yy;)classArrayofPoints(public:Arrayo
8、fPoints(intsize)this-size=size;1-ArrayofPoints()1Point&element(intindex)assert(index=O&indexsize);/size为数组大小Pointpointsindex;returnpointsindex;private:intsize;1;int1.lDistance(intxl,intyl,intx2,inty2)retum(abs(xI-x2)+abs(y1-y2);intmain()(intcount;cout,PleaseenterthecountofPointscount;ArrayofPointsAr
9、rPoints(Count);ArrPoints.element(0).move(5,10);ArrPoints.element(I).move(15,20);coutThedistanceisendl;cout1.lDistance(ArrPoints.element(0).GetX(),A11*Points.element(0).GetY(),ArrPoints.element(1).GetX(),ArrPoints.element(I).GetY()endl;returnO;)4 .定义一个Date类,有整型数据成员year,month,day成员函数Display用于显示日期;定义Da
10、teTime类,从Date类公有派生而来,在DateTime类中增加数据成员hour,mimte,second,设计一个成员函数Display用于显示日期时间信息。参考程序:#inchideusingnamespacestd;classDatapublic:Data(intyeaintmonthjntday)this-year=year;this-month=year;this-day=day;cout,Data构造函数被调用!vvendl;)voidDisplay()COUtVVyealvv”年,vvmonthvv月ndaynB,hour=d;this-minute=e;this-secon
11、d=f;)voidDisplay()CoiItVVyearVV“年VVmOnthVV月,dayn日vvhourvv时vvminutevv”分,endl;)private:inthour,minute,second;);intmain()ZDataA(2016,11,24);/A.Display();DataTimea(2016,11,24,13,50,59);a.Display();returnO;)5 .如图所示:家电电脑相机.Zl智能手机设计图示中的4个类,其中:电脑类(COmPIltrer)具有frequency,os,ram(CPU主频,操作系统,内存容量)等属性,相机(Camera)
12、具有PiXeI(像素数,如500万,800万等),从电脑和相机共同派生出智能手机,智能手机(Smobilephone)具有size(屏幕尺寸,如4.5寸,5.5寸等)。家电(Appliance)拥有brand,price(品牌,价格)等属性。构造智能手机类的一个实例,并通过该实例调用其成员ShoWO显示其继承自基类和自身的所有属性信息。参考程序:#incliideusingnamespacestd;classAppliancepublic:ApplianceCcharc,floatd)this-brand=c;this-price=d;)-Appliance()charGetbrand()re
13、turnbrand;)floatGctprice()returnprice;private:charbrand;floatprice;);classCoinputerjvirtualpublicAppliance(public:Computcr(floata,charb,intc,chard,floate):Appliancc(d,e)this-frequency=a;this-os=b;this-ran=c;)Computer()floatGetfrequency()returnfrequency;)charGetos()returnos;)intGetran()returnran;)private:11oatfrequency;charos;intran;);classCamerazvirtualApplianccpublic:Camera(inta,chardJloate):Appliancc(d,e)this-pixel=a;)intGetpixel()returnpixel;)private:intpixel;);classSmobilephonezpublicComputcpublicCamerapublic:Smobilephonc(floata,charb,i11tc,char