版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验12-继承与派生(实验思考 答案)实验 12 继承与派生三、实验思考1以下程序拟输出信息:class oneclass twoclass three请在不添加语句的情况下,改正程序中的错误,使之能正确输出信息。#include<iostream>using namespace std;class onepublic :void output ()cout<<"class one"<<endl;class two:public onepublic:void output()output();cout<<"class
2、 two"<<endl;class three:public twopublic:void output()output();cout<<"class three"<<endl;void main() three A;A.output();修改为:#include<iostream>using namespace std;class onepublic :void output ()cout<<"class one"<<endl;class two:public onepu
3、blic:void output()one:output();cout<<"class two"<<endl;class three:public twopublic:void output()two:output();cout<<"class three"<<endl;void main() three A;A.three:output();2假设图书馆的图书包含书名、编号、作者属性, 读者包含姓名和借书证属性, 每位读者最多可借 5 本书,编写程序列出某读者的借书情况。#include<iostr
4、eam>using namespace std;class bookint id;char *bookname;char *author;public:book(int x,char *y,char *z)id=x;bookname=y;author=z;class reader int number;char *name;int borrowid5;public:reader(int x,char *y) int i;number=x;name=y;for(i=0;i<5;i+) borrowidi=0;borrow(int id)int i;(borrowidi=0)for(i
5、=0;i<5;i+) ifborrowidi=id;break;3设计一个汽车类vehicle ,包含的数据成员有车轮个数wheels 和车重 weight 。小车类 car是 vehicle 的私有派生类 ,其中包含载人数passenger_load。 卡车类 truck 是 vehicle 的私有派生类, 其中包含载人数passenger_load 和载重量 payload ,每个类都有相关数据的输出方法。提示: vehicle 类是基类,由它派生出 car 类和truck类,将公共的属性和方法放在vehicle类中。#include<iostream>using nam
6、espace std;class vehicle int wheels;int weight;public:vehicle(int x,int y) wheels=x;weight=y;void output1()cout<<"vehicle:"<<wheels<<weight<<endl;class car:private vehicleint passenger_load;public:car(int r,int a,int b):vehicle(a,b) passenger_load=r;void output() ou
7、tput1();cout<<passenger_load<<endl;class truck:private vehicleint passenger_load,payload;public:truck(int r,int p,int a,int b):vehicle(a,b) passenger_load=r;payload=p;void output() output1();cout<<passenger_load<<payload<<endl;4设计一个圆类circle 和一个桌子类table ,另设计一个圆桌类roundtab
8、le , 它是从前两个类派生的, 要求输出一个圆桌的高度、 面积和颜色等数据。#include<iostream>using namespace std;#define PI 3.14enum colorsRed,White,Black;class circle int r;colors c;public:circle(int x,colors co)r=x;c=co;double area() return PI*r*r;show1()cout<<area()<<endl;switch(c)case Red:cout<<"Red&qu
9、ot;<<endl;break;case White:cout<<"White"<<endl;break;case Black:cout<<"Black"<<endl;break;class table int heigh;public:table(int h) heigh=h;show2()cout<<heigh<<endl;class roundtable:public circle,public table public:roundtable(inta,colors
10、b,intc):circle(a,b),table(c)output()show1();show2();main()roundtable c1(2.0,Red,4);c1.output();5定义一个字符串类onestr ,包含一个存放字符串的成员变量, 能够通过构造函数初始化字符串, 通过成员函数显示字符串的内容。 在此基础上派生出 twostr 类,增加一个存放字符串的成员变量,并能通过派生类的构造函数传递参数, 初始化两个字符串, 通过成员函数进行两个字符串的合并以及输出。#include<iostream>#include<string>using namespace std;class onestrprotected:string p;public:onestr(string q) p=q;show() cout<<p<<endl;class twostr:publi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 蓝色扁平化国庆出游旅行度假活动策划
- 2025千题百炼-高考数学100个热点问题(一):第28炼 三角函数性质含答案
- 2024届陕西省陕西师范大附属中学中考英语对点突破模拟试卷含答案
- 模拟电子技术基础 课件 (张菁)第7、8章 信号产生电路、功率放大电路
- 2024年高性能预应力钢丝相关公司行业营销方案
- 国电系统-北京市-2023年《信息安规》科目 单选题+多选题+判断题+简答题真题冲刺卷下半年B卷
- 2024年新闻采辑行业营销策略方案
- 2024年超精过滤设备行业营销策略方案
- 黑龙江省(齐黑大地区)2023年初中英语升学模拟大考卷试题(三)
- 高中物理选修3-2月考卷
- 2024年北京市劳动合同经典版(三篇)
- 中医病历书写基本规范
- JTG∕T F30-2014 公路水泥混凝土路面施工技术细则
- GB/T 3683-2023橡胶软管及软管组合件油基或水基流体适用的钢丝编织增强液压型规范
- 硬笔书法全册教案共20课时
- 七年级上期末动点问题专题(附答案)
- 浅谈初中生遵守和服从纪律意识的培养
- 一年级上册数学课件-第一单元可爱的校园|北师大版 (共24张PPT)
- 贺中秋迎国庆八月十五庆中秋节日介绍宣传PPT模板
- 建筑施工现场安全生产风险点清单目录(房屋建筑工程类)
- 儿科常见疾病诊疗指南与操作规范
评论
0/150
提交评论