




免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 住宅租赁安全协议(租赁房屋安全维护及责任协议)
- 离婚车辆处置协议:使用与归属明确划分合同
- 企业与劳动者签订劳动合同的劳动合同签订与备案流程
- 2025京东电商平台店铺租赁合同示范文本
- 砂石加工场地租赁与节能减排技术改造合同
- 行政合同在社会保障体系中的功能与作用研究
- 智能监测技术在农田水利中的应用-洞察及研究
- 社交媒体平台对鞋帽零售市场的影响研究-洞察及研究
- 2025融资租赁合同协议样本
- 2025-2030封装晶体振荡器技术演进与市场竞争格局分析报告
- 6.1 初步认识分数(课件 )数学青岛五四版三年级上册(新教材)
- GPS的课件教学课件
- 肺栓塞考试题及答案
- 2024法考主观题真题及答案
- 综合实践 探索年月日的秘密(教案)北师大版数学三年级上册
- 2025年医师三基考试试题及答案(上半年)
- 《彩虹》课件 部编版语文二年级上册
- 基孔肯雅热主题班会课件
- 2025年全国企业员工全面质量管理知识竞赛试题及答案
- 锁骨下盗血综合征伴锁骨下动脉闭塞的护理查房
- 磷化铝管理办法
评论
0/150
提交评论