实验12继承与派生实验思考答案.docx
- 文档编号:12535380
- 上传时间:2023-04-20
- 格式:DOCX
- 页数:9
- 大小:15.66KB
实验12继承与派生实验思考答案.docx
《实验12继承与派生实验思考答案.docx》由会员分享,可在线阅读,更多相关《实验12继承与派生实验思考答案.docx(9页珍藏版)》请在冰豆网上搜索。
实验12继承与派生实验思考答案
实验12--继承与派生(实验思考答案)
实验12继承与派生
三、实验思考
1.以下程序拟输出信息:
classone
classtwo
classthree
请在不添加语句的情况下,改正程序中的错误,使之能正确输出信息。
#include
usingnamespacestd;
classone
{public :
voidoutput(){cout<<"classone"< }; classtwo: publicone {public: voidoutput() {output(); cout<<"classtwo"< } }; classthree: publictwo {public: voidoutput() {output(); cout<<"classthree"< } }; voidmain() {threeA; A.output(); } 修改为: #include usingnamespacestd; classone {public: voidoutput(){cout<<"classone"< }; classtwo: publicone {public: voidoutput() {one: : output(); cout<<"classtwo"< } }; classthree: publictwo {public: voidoutput() {two: : output(); cout<<"classthree"< } }; voidmain() {threeA; A.three: : output(); } 2.假设图书馆的图书包含书名、编号、作者属性,读者包含姓名和借书证属性,每位读者最多可借5本书,编写程序列出某读者的借书情况。 #include usingnamespacestd; classbook { intid; char*bookname; char*author; public: book(intx,char*y,char*z) { id=x; bookname=y; author=z; } }; classreader {intnumber; char*name; intborrowid[5]; public: reader(intx,char*y) {inti; number=x; name=y; for(i=0;i<5;i++) borrowid[i]=0; } borrow(intid) {inti; for(i=0;i<5;i++) if(borrowid[i]==0){borrowid[i]=id;break;} } }; 3.设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。 小车类car是vehicle的私有派生类,其中包含载人数passenger_load。 卡车类truck是vehicle的私有派生类,其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。 提示: vehicle类是基类,由它派生出car类和truck类,将公共的属性和方法放在vehicle类中。 #include usingnamespacestd; classvehicle {intwheels; intweight; public: vehicle(intx,inty) {wheels=x; weight=y; } voidoutput1() {cout<<"vehicle: "< } }; classcar: privatevehicle { intpassenger_load; public: car(intr,inta,intb): vehicle(a,b) {passenger_load=r; } voidoutput() {output1(); cout< } }; classtruck: privatevehicle { intpassenger_load,payload; public: truck(intr,intp,inta,intb): vehicle(a,b) {passenger_load=r; payload=p; } voidoutput() {output1(); cout< } }; 4.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。 #include usingnamespacestd; #definePI3.14 enumcolors{Red,White,Black}; classcircle {intr; colorsc; public: circle(intx,colorsco) {r=x; c=co; } doublearea() {returnPI*r*r; } show1() {cout< switch(c) {caseRed: cout<<"Red"< caseWhite: cout<<"White"< caseBlack: cout<<"Black"< } } }; classtable {intheigh; public: table(inth) {heigh=h; } show2() {cout< } }; classroundtable: publiccircle,publictable {public: roundtable(inta,colorsb,intc): circle(a,b),table(c){} output() {show1(); show2(); } }; main() {roundtablec1(2.0,Red,4); c1.output(); } 5.定义一个字符串类onestr,包含一个存放字符串的成员变量,能够通过构造函数初始化字符串,通过成员函数显示字符串的内容。 在此基础上派生出twostr类,增加一个存放字符串的成员变量,并能通过派生类的构造函数传递参数,初始化两个字符串,通过成员函数进行两个字符串的合并以及输出。 #include #include usingnamespacestd; classonestr {protected: stringp; public: onestr(stringq) {p=q; } show() {cout< } }; classtwostr: publiconestr {stringr; public: twostr(strings,stringt): onestr(s) {r=t; } unite() {p=p+r; cout< } }; main() {twostrss("abc","def"); ss.unite(); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 12 继承 派生 思考 答案