面向对象实验_第1页
面向对象实验_第2页
面向对象实验_第3页
面向对象实验_第4页
面向对象实验_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上面向对象程序设计实验六实验报告班级 学号 姓名 1实验目的:(1) 掌握虚函数的定义和使用方法,理解虚函数在面向对象程序设计中的意义;(2) 理解虚函数在类的继承层次中的作用,虚函数的引入对程序运行时的影响,能够对使用虚函数的简单程序写出程序结果。2实验任务:1、定义一个表示平面上的点的类Point,实现一个show()函数输出其坐标,由它派生出一个表示平面上的圆的Circle类,添加一个半径成员,实现show()函数输出其坐标和半径。在主函数中定义一个基类的指针,用这个指针依次指向一个Point类的对象和一个Circle类的对象,分别调用show()函数输出对象的属

2、性,Point类对象输出点的坐标,Circle类对象输出圆点坐标和圆的半径。 2、求几何体:长方体和圆柱体的体积。具体要求如下:(1) 设计一个立体图形类(CStereoShape类),并满足如下要求:CStereoShape类有一个纯虚函数GetArea,能够获取立方体的表面积。CStereoShape类有一个纯虚函数GetVolume,能够获取立方体的体积。(2) 设计一个立方体类(CCube类),该类继承于CStereoShape类,并满足如下要求:l CCube类有一个带参数的构造函数,其参数分别为立方体的长、宽、高,默认值均为0。l 用一个成员函数put来实现对立方体长、宽、高的设置

3、。l 重载CStereoShape类的GetArea和GetVolume,分别完成立方体的表面积和体积的计算。(3) 设计一个球体类(CSphere),该类继承于CStereoShape类,并满足如下要求:l CSphere类有一个带参数的构造函数,其参数对应于球体的半径,默认值均为0。l 用一个成员函数put来实现对球体半径的设置。l 重载CStereoShape类的GetArea和GetVolume,分别完成球体的表面积和体积的计算。(4) 在主函数完成测试,完成如下工作:3 程序清单:任务一:/*main.cpp*/#include <iostream>#include &q

4、uot;Point.h"#include "Circle.h"using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char* argv) Point t(2,1),*p3; Circle c(3,4,4);p0=&t;p0->show();p1=&c;p1->show();return 0;/*

5、 Point.h*/#ifndef POINT_H#define POINT_Hclass Pointpublic:Point(int=0,int=0);virtual void show();protected:int x,y;#endif/* Point.cpp*/#include "Point.h"#include <iostream>using namespace std;Point:Point(int a,int b)x=a;y=b;void Point:show()cout<<"("<<x<<&

6、quot;,"<<y<<")"<<endl; /* Circle.h*/#ifndef CIRCLE_H#define CIRCLE_H#include "Point.h"class Circle : public Pointpublic:Circle(int x,int y,int a):Point(x,y) r=a; void show();protected:int r;#endif/* Circle.cpp*/#include "Circle.h"#include <iost

7、ream>using namespace std;void Circle:show()Point:show();cout<<"半径为:"<<r<<endl;任务二:/*main.cpp*/#include <iostream>#include "CStereoShape.h"#include "CSphere.h"#include "CCube.h"using namespace std;/* run this program using the console

8、 pauser or add your own getch, system("pause") or input loop */int main(int argc, char* argv) CStereoShape *p3; CCube b;p0=&b;b.put();p0->GetArea();p0->GetVolume();cout<<endl; CSphere s;p1=&s;s.put();p1->GetArea();p1->GetVolume(); return 0;/* CStereoShape.h*/#ifnd

9、ef CSTEREOSHAPE_H#define CSTEREOSHAPE_Hclass CStereoShapepublic:virtual double GetArea()=0;virtual double GetVolume()=0; protected:;#endif/* CCube.h*/#ifndef CCUBE_H#define CCUBE_H#include "CStereoShape.h"class CCube : public CStereoShapepublic:CCube(double=0,double=0,double=0);void put();

10、double GetArea();double GetVolume(); protected:double length,width,high;#endif/*CCube.cpp*/#include "CCube.h"#include <iostream>using namespace std;CCube:CCube(double a,double b,double c) length=a;width=b;high=c;void CCube:put()cout<<"请输入长、宽、高:"cin>>length>&g

11、t;width>>high; double CCube:GetArea()double s;s=2*(length*width+length*high+width*high);cout<<"长方体的表面积为:"<<s<<endl;double CCube:GetVolume()cout<<"长方体的体积为:"<<length*width*high<<endl;/*CSphere.h*/#ifndef CSPHERE_H#define CSPHERE_H#include &

12、quot;CStereoShape.h"class CSphere : public CStereoShapepublic:CSphere(double=0);void put();double GetArea();double GetVolume(); protected:double r;#endif/* CSphere.cpp*/#include "CSphere.h"#include <iostream>using namespace std;CSphere:CSphere(double a)r=a;void CSphere:put()cout

13、<<"请输入球的半径:"cin>>r; double CSphere:GetArea()cout<<"球体的表面积为:"<<4*3.14*r*r<<endl;double CSphere:GetVolume()cout<<"球体的体积为:"<<4/3*3.14*r*r*r<<endl;4 运行结果:任务一:任务二:5 总结或感悟: 任务一中Point类的头文件内设计一个虚函数的输出show函数,Circle类继承Point类里面的点坐标,同时增加了半径的变量。主函数中利用一个数组指针来分别指向定义的变量,并实现输出。任务二在CStereoShap

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论