实验12--继承与派生(实验思考答案)_第1页
实验12--继承与派生(实验思考答案)_第2页
实验12--继承与派生(实验思考答案)_第3页
实验12--继承与派生(实验思考答案)_第4页
实验12--继承与派生(实验思考答案)_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论