欢迎来到天天文库
浏览记录
ID:57263884
大小:47.09 KB
页数:7页
时间:2020-08-07
《面向对象实验.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、《面向对象程序设计》实验六实验报告班级学号姓名1实验目的:(1)掌握虚函数的定义和使用方法,理解虚函数在面向对象程序设计中的意义;(2)理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。2实验任务:1、定义一个表示平面上的点的类Point,实现一个show()函数输出其坐标,由它派生出一个表示平面上的圆的Circle类,添加一个半径成员,实现show()函数输出其坐标和半径。在主函数中定义一个基类的指针,用这个指针依次指向一个Point类的对象和一个Circl
2、e类的对象,分别调用show()函数输出对象的属性,Point类对象输出点的坐标,Circle类对象输出圆点坐标和圆的半径。2、求几何体:长方体和圆柱体的体积。具体要求如下:(1)设计一个立体图形类(CStereoShape类),并满足如下要求:CStereoShape类有一个纯虚函数GetArea,能够获取立方体的表面积。CStereoShape类有一个纯虚函数GetVolume,能够获取立方体的体积。(2)设计一个立方体类(CCube类),该类继承于CStereoShape类,并满足如下要求:lCCube类有一
3、个带参数的构造函数,其参数分别为立方体的长、宽、高,默认值均为0。l用一个成员函数put来实现对立方体长、宽、高的设置。l重载CStereoShape类的GetArea和GetVolume,分别完成立方体的表面积和体积的计算。(3)设计一个球体类(CSphere),该类继承于CStereoShape类,并满足如下要求:lCSphere类有一个带参数的构造函数,其参数对应于球体的半径,默认值均为0。l用一个成员函数put来实现对球体半径的设置。l重载CStereoShape类的GetArea和GetVolume,分别
4、完成球体的表面积和体积的计算。(4)在主函数完成测试,完成如下工作:3程序清单:任务一:/*main.cpp*/#include#include"Point.h"#include"Circle.h"usingnamespacestd;/*runthisprogramusingtheconsolepauseroraddyourowngetch,system("pause")orinputloop*/intmain(intargc,char**argv){Pointt(2,1),*p[3];Circ
5、lec(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*/#include"Point.h"#includeusingnamespacestd;Point::Point(
6、inta,intb){x=a;y=b;}voidPoint::show(){cout<<"("<7、cle.h"#includeusingnamespacestd;voidCircle::show(){Point::show();cout<<"半径为:"<#include"CStereoShape.h"#include"CSphere.h"#include"CCube.h"usingnamespacestd;/*runthisprogramusingtheconsolepauseroraddyourowng8、etch,system("pause")orinputloop*/intmain(intargc,char**argv){CStereoShape*p[3];CCubeb;p[0]=&b;b.put();p[0]->GetArea();p[0]->GetVolume();cout<Ge
7、cle.h"#includeusingnamespacestd;voidCircle::show(){Point::show();cout<<"半径为:"<#include"CStereoShape.h"#include"CSphere.h"#include"CCube.h"usingnamespacestd;/*runthisprogramusingtheconsolepauseroraddyourowng
8、etch,system("pause")orinputloop*/intmain(intargc,char**argv){CStereoShape*p[3];CCubeb;p[0]=&b;b.put();p[0]->GetArea();p[0]->GetVolume();cout<Ge
此文档下载收益归作者所有