免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
电子信息学院实验报告书课 程 名: 面向对象程序设计 题 目: 实验8 多态性 实验类别: 设计 班 级: BX1004 学 号: 101003040420 姓 名: 施程程 评语:实验态度:认真( ) 一般( ) 差( )实验结果:正确( ) 部分正确( )错( )实验理论:掌握( ) 熟悉( ) 了解( ) 不懂( )操作技能:强( ) 一般( ) 差( )实验报告:好( ) 一般( ) 差( )成绩: 指导教师: 陈群贤 批阅时间: 年 月 日一实验目的1 理解并掌握利用虚函数实现动态多态性和编写通用程序的方法2 掌握纯虚函数和抽象类的使用二实验内容1. 在例6.1程序基础上作一些修改。定义Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱体)类。将类的定义部分分别作为3个头文件,对它们的成员函数的声明部分分别作为3个源文件(.cpp文件),在主函数中用#include命令把它们包含进来,形成一个完整的程序,并上机运行。2. 写一个程序,定义抽象基类Shape,由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)、Triangle(三角形),用一个函数printArea分别输出以上三者的面积,3个图形的数据在定义对象时给定。三实验结果(1)程序:point.h函数:class Pointpublic:Point(float x=0,float y=0);void setPoint(float,float);float getX()constreturn x;float getY()constreturn y;friend ostream & operator(ostream &,const Point &);protected:float x,y;point.cpp函数:#include#include point.hPoint:Point(float a,float b)x=a;y=b;void Point:setPoint(float a,float b)x=a;y=b;ostream & operator(ostream &output,const Point &p)outputp.x,p.yendl;return output;circle.h函数:class Circle:public Pointpublic:Circle(float x=0,float y=0,float r=0);void setRadius(float);float getRadius()const;float area() const;friend ostream &operator(ostream &,const Circle &);protected:float radius;circle.cpp函数:#include#includepoint.h#includecircle.hCircle:Circle(float a,float b,float r):Point(a,b),radius(r)void Circle:setRadius(float r)radius=r;float Circle:getRadius() constreturn radius;float Circle:area() const return 3.14159*radius*radius;ostream &operator(ostream &output,const Circle &c)outputCenter=c.x,c.y,r=c.radius,area=c.area()endl;return output;cylinder.h函数:class Cylinder:public Circlepublic:Cylinder(float x=0,float y=0,float r=0,float h=0);void setHeight(float);float getHeight() const;float area() const;float volume() const;friend ostream& operator(ostream&,const Cylinder&);protected:float height;cylinder.cpp函数:#include#includepoint.h#includecircle.h#includecylinder.hCylinder:Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h)void Cylinder:setHeight(float h)height=h;float Cylinder:getHeight() const return height;float Cylinder:area() constreturn 2*Circle:area()+2*3.14159*radius*height;float Cylinder:volume() constreturn Circle:area()*height;ostream &operator(ostream &output,const Cylinder& cy)outputcenter=cy.x,cy.y,r=cy.radius,h=cy.heightnarea=cy.area(),volume=cy.volume()endl;return output;mian函数:#include#includepoint.h#includecircle.h#includecylinder.hint main()Cylinder cy1(3.5,6.4,5.2,10);coutnoriginal cylinder:nx=cy1.getX(),y=cy1.getY(),r=cy1.getRadius(),h=cy1.getHeight()narea=cy1.area(),volume=cy1.volume()endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);coutnnew cylinder:ncy1;Point &pRef=cy1;coutnpRef as a Point:pRef;Circle &cRef=cy1;coutncRef as a Circle:cRef;return 0;运行结果:(2)程序:#include using namespace std;class Shapepublic: virtual double area() const =0; ;class Circle:public Shapepublic:Circle(double r):radius(r) virtual double area() const return 3.14*radius*radius; protected: double radius; ;class Rectangle:public Shapepublic: Rectangle(double l,double w):length(l),width(w) virtual double area() const return length*width; protected: double length,width; ;class Triangle:public Shapepublic: Triangle(double l,double h):length(l),height(h) virtual double area() const return 0.5*length*height; protected: double length,height; ;void printArea(const Shape &s)couts.area()endl; int main() Circle circle(5); coutarea of circle =; printArea(circle); Rectangle rectangle(6,4); coutarea of rectangle =; printArea(rectangle); Triangle triangle(4,8); coutarea of triangle =; printArea(triangle); return 0;运行结果:4、 实验体会我们C+的实验课快步入尾声了,才发现时间过得好快。这次实验要求我们掌握的是利用虚函数实现动态多态性和编写通用程序的方法,还有掌握纯虚函数和抽象类的使用。第一题
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 勘察服务方案
- 2026年施工工地项目经理的年终总结
- 三亚防腐地板面层施工方案
- 不同胃管置入方式对早产儿喂养结局的影响
- 高标准农田建设项目-安全文明施工保证措施
- 锅炉水质化验方案
- 2026年景区讲解员五级职业技能考试题(含答案)
- 2026年言语治疗师招聘真题(附答案)
- 2026企业可持续发展(ESG)经理人调研报告
- 2026年潍坊天立学校学科教师招聘笔试备考题库及答案解析
- 《毛泽东思想概论》 课件 第5、6章 社会主义建设理论、革命军队建设和军事战略理论
- 社区底商招商方案
- 实施指南(2025)《JC-T 2822-2024 水泥替代原料》
- 2025年民航招飞心理测试试题及答案
- 内燃机车运用与检修课件
- 钢结构楼梯施工合同协议书
- 中心静脉置管护理风险评估
- 2025年货币鉴定师(初级)职业资格认定参考试题库(含答案)
- 第二产程的护理措施
- 工会探视慰问制度(4篇)
- 《湖南省房屋建筑和市政工程消防质量控制技术标准》
评论
0/150
提交评论