版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验六继承和派生姓名:学号成绩评语:实验目的掌握单继承和多重继承下派生类的定义方法理解基类成员在不同的继承方式下不同的访问属性正确定义派生类的构造函数与析构函数,理解定义一些个派生类对象时各个构造函数、析构函数被调用的顺序;学习利用虚基类解决二义性问题。实验任务1.定义一个车基类vehicle,派生出自行车类bicycle和汽车类car,又以自行车和汽车类为基类共同派生出摩托车类motorcycle,每个类都要定义带参数的构造函数。对自行车类继承车基类的方式分别用private,protected,public,观察基类成员在派生类中的访问属性;观察自行车类、汽车类和摩托类对象定义时构造、析构函数的调用顺序。具体完成以下要求:(1)定义基类vehicle,具有两个保护成员变量:maxspeed,weight,有3个公有的成员函数:run(),stop,show(),以及带参数的构造函数、析构函数;再定义一个从vehicle公有继承的bicycle类,增加height保护属性的成员变量,定义bicycle类的构造函数、析构函数,改选show()函数,用于输出本类中的完整信息。Main()函数中定义bicycle类对象,观察构造函数和析构函数的执行顺序,以及各成员函数的调用。请用跟踪的方法观察程序运行的每一步究竟调用的是哪一个函数。(2)在上一步的基础上,将继承方式分别修改为protected和private,再重新编译,观察这时的报错信息并能解释。修改程序使程序正确运行。(3)将bicycle类的继承方式恢复为public,代码回到(1)的状态,再在bicycle类下面增加下第二层汽车类car的定义,car也是公有继承基类vehicle,其中增加了一个保护成员变量seatnum,表示汽车有几个座位,其定义方式与类bicycle类似。主函数中定义该类对象,观察运行结果。(4)在上一步基础上,再定义一个第三层类motorcycle,该类以公有方式继承了第二层的bicycle和car类。定义其构造函数,要调用两个直接基类的构造函数,再改选函数show(),输出所有四个成员变量的信息。主函数中只定义类motorcycle的对象并调用相应的函数,程序进行编译,会产生错误和警告,因为存在二义性问题,请采用把vehicle设置为虚基类人方法消除二义性,修改程序,观察运行结果。
在继承过程中,注意,如果不把vehicle设置为虚基类,会有什么问题?编程试试看。三、实验代码与调试结果(结果请截图)1#include<iostream>usingnamespacestd;classvehicle{protected:doublemaxspeed;doubleweight;public:vehicle(doublea,doubleb){ maxspeed=a; weight=b; cout<<"vehicle'sconstructorcalled"<<endl;} voidrun(){ cout<<maxspeed<<endl; } voidstop(){ maxspeed=0; cout<<maxspeed<<endl;} voidshow(){ cout<<maxspeed<<""<<weight<<endl; } doublegetmaxspeed(){returnmaxspeed;} doublegetweight(){returnweight;} ~vehicle(){ cout<<"vehicle'sdestructorcalled"<<endl;}};classbicycle:publicvehicle{protected:doubleheight;public:bicycle(doublea,doubleb,doublec):vehicle(a,b){height=c; cout<<"bicycle'sconstructorcalled"<<endl;} ~bicycle(){cout<<"bicycle'sdestructorcalled"<<endl;} voidshow(){ cout<<getmaxspeed()<<endl; cout<<getweight()<<endl; cout<<height<<endl;}};intmain(){ bicycleb(25,20,30); b.show();return0;}2errorC2274:'function-stylecast':illegalasrightsideof'.'operator原因:保护继承和私有继承都不能在类外调用基类的公有成员或保护成员3#include<iostream>usingnamespacestd;classvehicle{protected:doublemaxspeed;doubleweight;public:vehicle(doublea,doubleb){ maxspeed=a; weight=b; cout<<"vehicle'sconstructorcalled"<<endl;} voidrun(){ cout<<maxspeed<<endl; } voidstop(){ maxspeed=0; cout<<maxspeed<<endl;} voidshow(){ cout<<maxspeed<<""<<weight<<endl; } doublegetmaxspeed(){returnmaxspeed;} doublegetweight(){returnweight;} ~vehicle(){ cout<<"vehicle'sdestructorcalled"<<endl;}};classbicycle:publicvehicle{protected:doubleheight;public:bicycle(doublea,doubleb,doublec):vehicle(a,b){height=c; cout<<"bicycle'sconstructorcalled"<<endl;} ~bicycle(){cout<<"bicycle'sdestructorcalled"<<endl;} voidshow(){ cout<<getmaxspeed()<<endl; cout<<getweight()<<endl; cout<<height<<endl;}};classcar:publicvehicle{protected:intseatnum;public:car(doublea,doubleb,intc):vehicle(a,b){ seatnum=c; cout<<"car'sconstructorcalled"<<endl;} ~car(){cout<<"car'sdestructorcalled"<<endl;} voidshow(){ cout<<getmaxspeed()<<endl;cout<<getweight()<<endl; cout<<seatnum<<endl;}};intmain(){ bicycleb(25,20,30); b.show(); carc(10,20,30); c.show();return0;}4#include<iostream>usingnamespacestd;classvehicle{protected:doublemaxspeed;doubleweight;public:vehicle(doublea,doubleb){ maxspeed=a; weight=b; cout<<"vehicle'sconstructorcalled"<<endl;} voidrun(){ cout<<maxspeed<<endl; } voidstop(){ maxspeed=0; cout<<maxspeed<<endl;} voidshow(){ cout<<maxspeed<<""<<weight<<endl; } doublegetmaxspeed(){returnmaxspeed;} doublegetweight(){returnweight;} ~vehicle(){ cout<<"vehicle'sdestructorcalled"<<endl;}};classbicycle:virtualpublicvehicle{protected:doubleheight;public:bicycle(doublea,doubleb,doublec):vehicle(a,b){height=c; cout<<"bicycle'sconstructorcalled"<<endl;} ~bicycle(){cout<<"bicycle'sdestructorcalled"<<endl;} voidshow(){ cout<<getmaxspeed()<<endl; cout<<getweight()<<endl; cout<<height<<endl;} doublegetheight(){returnheight;}};classcar:virtualpublicvehicle{protected:intseatnum;public:car(doublea,doubleb,intc):vehicle(a,b){ seatnum=c; cout<<"car'sconstructorcalled"<<endl;} ~car(){cout<<"car'sdestructorcalled"<<endl;} voidshow(){ cout<<getmaxspeed()<<endl;cout<<getweight()<<endl; cout<<seatnum<<endl;} intgetseatnum(){returnseatnum;}};classmotorcycle:publicbicycle,publiccar{public: motorcycle(doublea,doubleb,doublec,intd):vehicle(a,b),bicycle(a,b,c),car(a,b,d){cout<<"motorcycle'sconstructorcalled"<<endl;} voidshow(){
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 陕西省恒口高级中学2025年高三下学期四调考试物理试题(A4版含解析)含解析
- 陕西省汉中市汉台区2024-2025学年5月高考模练习(一)物理试题含解析
- 期中 (试题) -2024-2025学年人教精通版英语六年级上册
- 未漂白人造长丝机织物项目节能评估报告模板
- 幼儿园重阳节方案与总结
- 机械结构设计基础知识
- 工程施工合同(模板)
- 初一第一学期语文月考卷09
- 2024年江苏省小学教师资格证《小学综合素质》科目真题冲刺卷
- 建设项目可行性实施报告课程设计论文
- 南京大学中国语言文学(一级学科)博士研究生培养方案
- 郑大远程教育教学学位英语考试真题
- 妇产科学女性生殖系统解剖优秀课件
- 【山西】大同火山群国家地质公园旅游总体规划
- 符号检验教案课件
- 中国文旅特色小镇发展研究报告
- 小学体育与健康人教~年级全一册第三部分体育运动技能篮球行进间运球教学 省赛获奖
- 人教版小学音乐二年级上册《过新年》课件
- 起重作业吊装令
- 人教版九年级数学上册 第二十二章 二次函数 复习课件
- 流行性感冒预防及治疗(共29张)课件
评论
0/150
提交评论