




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.实验三 虚函数与多态、纯虚函数 一.实验目的1. 在掌握继承与派生关系的基础上,进一步理解虚函数与多态性的关系,实现运行时的多态。 2. 学会定义和使用纯虚函数 二、实验内容1范例:了解单接口,多方法的概念。现有称为figure的基类,存放了各二维对象(三角形、矩形和圆形三个类)的各维数据,set_dim()设置数据,是标准成员函数。show_area()为虚函数,因为计算各对象的面积的方法是不同的。【程序】#include using namespace std;class figureprotected:double x,y;public:void set_dim(double i,double j=0) x=i; y=j; virtual void show_area() coutNo area computation defined for this class.n;class triangle:public figurepublic:void show_area() coutTriangle with height x and base y has an area of x*0.5*y endl; ;class square:public figurepublic:void show_area() coutSquare with dimensions x and y has an area of x*y endl; ;class circle:public figurepublic: void show_area() coutCircle with radius x has an area of 3.14159*x*xset_dim(10.0,5.0);p-show_area();p=&s;p-set_dim(10.0,5.0);p-show_area();p=&c; p-set_dim(10.0);p-show_area(); return 0; 【要求】(1) 建立工程,录入上述程序,调试运行并记录运行结果。 (2) 修改上述程序,将virtual void show_area()中的virtual去掉,重新调试运行观察结果有何变化?为什么?在不使用关键字virtual后,基类指针p对show-area的访问p-show_area()没有针对所指对象的类型调用不同的函数,而是直接根据p的类型调用了基类的成员函数show-area。(3) 修改上述程序入口函数,使其动态建立三角形、矩形和圆形3个对象,通过基类指针访问这3个对象,然后释放这3个对象。#include using namespace std;class figureprotected:double x,y;public:void set_dim(double i,double j=0) x=i; y=j; virtual void show_area() coutNo area computation defined for this class.n;class triangle:public figurepublic:void show_area() coutTriangle with height x and base y has an area of x*0.5*y endl; ;class square:public figurepublic:void show_area() coutSquare with dimensions x and y has an area of x*y endl; ;class circle:public figurepublic: void show_area() coutCircle with radius x has an area of 3.14159*x*xset_dim(10.0,5.0);p-show_area();p=p2;p-set_dim(10.0,5.0);p-show_area();p=p3;p-set_dim(10.0);p-show_area();delete p1;delete p2;delete p3;(4) 修改类定义中的析构函数,使之适应用户动态定义对 2、使用纯虚函数和抽象类对实验二中的题1进行改进。 #include using namespace std;class figureprotected:double x,y;public:void set_dim(double i,double j=0) x=i; y=j; virtual void show_area() coutNo area computation defined for this class.n;/在此处将基类的析构函数声明为虚析构就OK了virtual figure()/;class triangle:public figurepublic:void show_area() coutTriangle with height x and base y has an area of x*0.5*y endl; ;class square:public figurepublic:void show_area() coutSquare with dimensions x and y has an area of x*y endl; ;class circle:public figurepublic: void show_area() coutCircle with radius x has an area of 3.14159*x*xset_dim(10.0,5.0);p-show_area();p=&s;p-set_dim(10.0,5.0);p-show_area();p=&c; p-set_dim(10.0);p-show_area(); return 0; 2、编程:自定义一个抽象类Element,提供显示、求面积等公共接口(虚函数),派生出Point、Line、Circle等图形元素类,并重新定义(override)这些虚函数,完成各自的任务。在这里,Element是抽象基类,它不能提供具体的显示操作,应将其成员函数定义为纯虚函数。只有采用指向基类的指针或对基类的引用进行调用,实现的才是动态绑定,完成运行时的多态性。#include using namespace std;const double PI=3.14159;class Elementpublic:virtual const char* Name() const =0;virtual double Area() const =0;class Point:public Elementprotected:double x,y;public:Point(double xv=0,double yv=0):x(xv),y(yv)virtual const char* Name() constreturn Point;virtual double Area() constreturn 0;class Line:public Elementprotected:double length;public:Line(double l=0):length(l)virtual const char* Name() constreturn Line;virtual double Area() constreturn 0;class Circle:public Elementprotected:double radius;public:Circle(double r):radius(r)virtual const char* Name() constreturn Circle;virtual do
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年苏州中学附属三亚学校面向社会公开招聘教师模拟试卷有完整答案详解
- 2025贵州医科大学附属口腔医院引进高层次人才考前自测高频考点模拟试题及答案详解参考
- 2025年中国货物雪橇行业市场分析及投资价值评估前景预测报告
- 2025江苏泰州市中西医结合医院招聘高层次卫生专业技术人才5人考前自测高频考点模拟试题有答案详解
- 2025贵州罗甸县第一医共体板庚分院招聘合同制专业技术人员考前自测高频考点模拟试题及答案详解(夺冠系列)
- 2025广西桂林工程职业学院人才招聘模拟试卷完整参考答案详解
- 2025年中国环孢素A原料药行业市场分析及投资价值评估前景预测报告
- 2025安徽六安市中医院招聘13人考前自测高频考点模拟试题及答案详解(易错题)
- 2025年河北石家庄法商中等专业学校公开招聘教师37名模拟试卷附答案详解
- 2025河北秦皇岛市公安医院招聘14人模拟试卷及参考答案详解1套
- 2025菏投热电(巨野)有限公司面向市属企业(内部)选聘运维人员60人笔试参考题库附带答案详解(10套)
- 黑龙江介绍课件
- 2025至2030中国汽车A柱行业项目调研及市场前景预测评估报告
- 2026年高考英语专题复习:必背近10高考英语高频词汇表
- 呼吸心跳骤停病人的护理查房
- 广州市市政工程主要项目概算指标及编制指引 (2021年)
- 关于体育的论文
- 第三届“皇家杯”职业院校宠物营养学知识竞赛考试题库(含答案)
- QGDW12505-2025电化学储能电站安全风险评估规范
- 研究生教材SPSS统计软件应用
- 2025年部编版新教材三年级上册《9.犟龟》教案
评论
0/150
提交评论