版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.实验三 虚函数与多态、纯虚函数 一.实验目的1. 在掌握继承与派生关系的基础上,进一步理解虚函数与多态性的关系,实现运行时的多态。 2. 学会定义和使用纯虚函数 二、实验内容1范例:了解单接口,多方法的概念。现有称为figure的基类,存放了各二维对象(三角形、矩形和圆形三个类)的各维数据,set_dim()设置数据,是标准成员函数。show_area()为虚函数,因为计算各对象的面积的方法是不同的。【程序】#include using namespace std;class figureprotected:double x,y;public:void set_dim(double i,do
2、uble 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 h
3、as 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 vo
4、id 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=
5、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 a
6、rea 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、使用纯虚函数和抽象类对实验二
7、中的题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() coutTriangl
8、e 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(
9、);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
10、;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 double Ar
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农村电商物流师能力水平认证试卷及答案
- 医学生理化学类:细胞色素课件
- 导游服务意识问卷调查试题冲刺卷
- 2025年出租车司机安全防范评价试题冲刺卷
- 天津市滨海新区大港油田一中2026年高三下学期四月调考物理试题含解析
- 2026年云南省丽江市重点中学高三3月联考(物理试题文)试题含解析
- 2025河南开封市万岁山游览区有限公司招聘笔试历年备考题库附带答案详解
- 2026届湖南省湖南师大附中高考语文试题命题比赛模拟试卷(22)含解析
- 2026年吉林省扶余市一中高三下第二次阶段(期中)语文试题含解析
- 2025江苏盐城市滨海县农旅集团招聘财务人员人员笔试参考题库附带答案详解
- 2026年山东胜利职业学院单招综合素质考试题库附答案解析
- 销售岗位合同范本与注意事项
- 不合格人员再培训制度
- 《世说新语》启发教学法示范教案
- 深圳海关公务员考试试题及答案
- 烫熨疗法课件
- 四川省2025年高职单招职业技能综合测试(中职类)计算机类试卷(含答案解析)
- 2025年采制样工岗位培训与考试题库采及答案
- 中国微生物肥项目创业投资方案
- 部队班组搜索战术课件
- 山东省潍坊市2025年中考数学真题附真题答案
评论
0/150
提交评论