面向对象实验6

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

《面向对象程序设计》实验六实验报告班级学号姓名1实验目的:(1)掌握虚函数的定义和使用方法,理解虚函数在面向对象程序设计中的意义;(2)理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。2实验任务:1、定义一个表示平面上的点的类Point,实现一个show()函数输出其坐标,由它派生出一个表示平面上的圆的Circle类,添加一个半径成员,实现show()函数输出其坐标和半径。在主函数中定义一个基类的指针,用这个指针依次指向一个Point类的对象和一个Circle类的对象,分别调用show()函数输出对象的属性,Point类对象输出点的坐标,Circle类对象输出圆点坐标和圆的半径。2、求几何体:长方体和圆柱体的体积。具体要求如下:(1)设计一个立体图形类(CStereoShape类),并满足如下要求:CStereoShape类有一个纯虚函数GetArea,能够获取立方体的表面积。CStereoShape类有一个纯虚函数GetVolume,能够获取立方体的体积。(2)设计一个立方体类(CCube类),该类继承于CStereoShape类,并满足如下要求:CCube类有一个带参数的构造函数,其参数分别为立方体的长、宽、高,默认值均为0。用一个成员函数put来实现对立方体长、宽、高的设置。重载CStereoShape类的GetArea和GetVolume,分别完成立方体的表面积和体积的计算。(3)设计一个球体类(CSphere),该类继承于CStereoShape类,并满足如下要求:CSphere类有一个带参数的构造函数,其参数对应于球体的半径,默认值均为0。用一个成员函数put来实现对球体半径的设置。重载CStereoShape类的GetArea和GetVolume,分别完成球体的表面积和体积的计算。(4)在主函数完成测试,完成如下工作:3程序清单:任务一:/*main.cpp*/#includeiostream#includePoint.h#includeCircle.husingnamespacestd;/*runthisprogramusingtheconsolepauseroraddyourowngetch,system(pause)orinputloop*/intmain(intargc,char**argv){Pointt(2,1),*p[3];Circlec(3,4,4);p[0]=&t;p[0]-show();p[1]=&c;p[1]-show();return0;}/*Point.h*/#ifndefPOINT_H#definePOINT_HclassPoint{public:Point(int=0,int=0);virtualvoidshow();protected:intx,y;};#endif/*Point.cpp*/#includePoint.h#includeiostreamusingnamespacestd;Point::Point(inta,intb){x=a;y=b;}voidPoint::show(){cout(x,y)endl;}/*Circle.h*/#ifndefCIRCLE_H#defineCIRCLE_H#includePoint.hclassCircle:publicPoint{public:Circle(intx,inty,inta):Point(x,y){r=a;}voidshow();protected:intr;};#endif/*Circle.cpp*/#includeCircle.h#includeiostreamusingnamespacestd;voidCircle::show(){Point::show();cout半径为:rendl;}任务二:/*main.cpp*/#includeiostream#includeCStereoShape.h#includeCSphere.h#includeCCube.husingnamespacestd;/*runthisprogramusingtheconsolepauseroraddyourowngetch,system(pause)orinputloop*/intmain(intargc,char**argv){CStereoShape*p[3];CCubeb;p[0]=&b;b.put();p[0]-GetArea();p[0]-GetVolume();coutendl;CSpheres;p[1]=&s;s.put();p[1]-GetArea();p[1]-GetVolume();return0;}/*CStereoShape.h*/#ifndefCSTEREOSHAPE_H#defineCSTEREOSHAPE_HclassCStereoShape{public:virtualdoubleGetArea()=0;virtualdoubleGetVolume()=0;protected:};#endif/*CCube.h*/#ifndefCCUBE_H#defineCCUBE_H#includeCStereoShape.hclassCCube:publicCStereoShape{public:CCube(double=0,double=0,double=0);voidput();doubleGetArea();doubleGetVolume();protected:doublelength,width,high;};#endif/*CCube.cpp*/#includeCCube.h#includeiostreamusingnamespacestd;CCube::CCube(doublea,doubleb,doublec){length=a;width=b;high=c;}voidCCube::put(){cout请输入长、宽、高:;cinlengthwidthhigh;}doubleCCube::GetArea(){doubles;s=2*(length*width+length*high+width*high);cout长方体的表面积为:sendl;}doubleCCube::GetVolume(){cout长方体的体积为:length*width*highendl;}/*CSphere.h*/#ifndefCSPHERE_H#defineCSPHERE_H#includeCStereoShape.hclassCSphere:publicCStereoShape{public:CSphere(double=0);voidput();doubleGetArea();doubleGetVolume();protected:doubler;};#endif/*CSphere.cpp*/#includeCSphere.h#includeiostreamusingnamespacestd;CSphere::CSphere(doublea){r=a;}voidCSphere::put(){cout请输入球的半径:;cinr;}doubleCSphere::GetArea(){cout球体的表面积为:4*3.14*r*rendl;}doubleCSphere::GetVolume(){cout球体的体积为:4/3*3.14*r*r*rendl;}4运行结果:任务一:任务二:5总结或感悟:任务一中Point类的头文件内设计一个虚函数的输出show函数,Circle类继承Point类里面的点坐标,同时增加了半径的变量。主函数中利用一个数组指针来分别指向定义的变量,并实现输出。任务二在CStereoShape基类中设计设计求表面积和求体积的两个纯虚函数,设计两个CCube和CSphere的派生类。主函数内通过指针数据分别指向派生类并实现输出。多态的设计可以把不同的子类对象都当作父类来看,可以屏蔽不同子类之间的差异,可通过需求不断的变化,让代码更加的简洁。刚开始写的时候,类的编写没什么大问题,但是在写主函数的时候会出现一些小问题,最后通过翻阅书本上面的知识点和例题,以及网上搜索多态的编写方法得以解决。

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功