专业做网站建设公,长沙做网站工作室,下载百度app到桌面,优秀购物网站CSDN的各位友友们你们好,今天千泽为大家带来的是燕山大学-面向对象程序设计实验-实验5 派生与继承#xff1a;单重派生-实验报告,接下来让我们一起进入c的神奇小世界吧,相信看完你也能写出自己的 实验报告!本系列文章收录在专栏 燕山大学面向对象设计报告中 ,您可以在专栏中找…CSDN的各位友友们你们好,今天千泽为大家带来的是燕山大学-面向对象程序设计实验-实验5 派生与继承单重派生-实验报告,接下来让我们一起进入c的神奇小世界吧,相信看完你也能写出自己的 实验报告!本系列文章收录在专栏 燕山大学面向对象设计报告中 ,您可以在专栏中找到其他章节如果对您有帮助的话希望能够得到您的支持和关注,我会持续更新的!实验六 派生与继承—多重派生6.1 实验目的1理解多重派生的定义2理解多重派生中构造函数与析构函数的调用顺序3理解多重派生中虚拟基类的作用6.2 实验内容6.2.1程序阅读1理解下面的程序并在VC6.0下运行查看结果回答程序后面的问题。class CBase1{public:CBase1(int a):a(a){coutbase1 structure...endl;}~CBase1(){coutbase1 destructure...endl;}void print(){coutaaendl;}protected:int a;};class CBase2{public:CBase2(int b):b(b){coutbase2 structure...endl;}~CBase2(){coutbase2 destructure...endl;}void print(){coutbbendl;}protected:int b;};class CDerive : public CBase1, public CBase2{public:CDerive(){coutderive structure...endl;}~CDerive(){coutderive destructure...endl;}void print(){CBase1::print();CBase2::print();b1.print();b2.print();coutccendl;}private:CBase1 b1;CBase2 b2;int c;};void main(){CDerive d;d.print();}
问题一改正以上程序中存在的错误并分析该程序的输出结果。答1没有引用头文件且未声明命名空间。2CBase1和CBase2类缺少默认的构造函数。分析main函数创建CDerive类对象d时CDerive类继承了CBase1和CBase2类因此输出了前两行。在CDerive类中又有两个成员对象b1和b2再次调用CBase1和CBase2的构造函数故有3、4行。随后CDerive构造函数中有输出第5行内容。6-10行为CDerive::print()函数中内容由于在调用时均为赋值因此变量内容不确定。后5行为析构与构造过程相反。2理解下面的程序并在VC6.0下运行查看结果回答程序后面的问题。#include iostreamclass CBase{public:CBase(int a):a(a){}int a;};class CDerive1 : public CBase{public:CDerive1(int a):CBase(a){}};class CDerive2 : public CBase{public:CDerive2(int a):CBase(a){}};class CDerive : public CDerive1,public CDerive2{public:CDerive(int a,int b):CDerive1(a),CDerive2(b){}};void main(){CDerive d(1,2);coutd.aendl;}
问题一在不改变原有程序意图的前提下分别用三种方法改正以上程序并使程序正确输出。答1方法一使用虚继承#include iostreamusing namespace std;class CBase {public:CBase(int a):a(a){}int a;};class CDerive1 : virtual public CBase{public:CDerive1(int a):CBase(a){}};class CDerive2 :virtual public CBase{public:CDerive2(int a):CBase(a){}};class CDerive : public CDerive1,public CDerive2{public:CDerive(int a,int b):CDerive1(a),CDerive2(b),CBase(b){}};int main(){CDerive d(1,2);coutd.aendl;return 0;}2添加输出函数print()#include iostreamusing namespace std;class CBase {public:CBase(int a):a(a){}int a;int print(){return a;}};class CDerive1 : public CBase{public:CDerive1(int a):CBase(a){}};class CDerive2 :public CBase{public:CDerive2(int a):CBase(a){}};class CDerive : public CDerive1,public CDerive2{public:CDerive(int a,int b):CDerive1(a),CDerive2(b){}int print(){return CDerive1::print();}};int main(){CDerive d(1,2);coutd.print()endl;return 0;}3获取变量时限定作用域#include iostreamusing namespace std;class CBase {public:CBase(int a):a(a){}int a;};class CDerive1 : public CBase{public:CDerive1(int a):CBase(a){}};class CDerive2 :public CBase{public:CDerive2(int a):CBase(a){}};class CDerive : public CDerive1,public CDerive2{public:CDerive(int a,int b):CDerive1(a),CDerive2(b){}int getA(){return CDerive1::a;}};int main(){CDerive d(1,2);coutd.getA()endl;return 0;}6.2.2 程序设计1建立普通的基类building用来存储一座楼房的层数、房间数以及它的总平方数。建立派生类house继承building并存储卧室与浴室的数量另外建立派生类office继承building并存储灭火器与电话的数目。设计一主函数来测试以上类的用法。答#includeiostreamusing namespace std;class building{protected:int floor;int room_num;int area;public:building(int a,int b,int c):floor(a),room_num(b),area(c){}void buildingInfo(){cout* 楼层:floorendl;cout* 房间数room_numendl;cout* 面积areaendl;}};class house:public building{private:int sleep_num;int wash_num;public:house(int a,int b,int c,int d,int e):building(a,b,c),sleep_num(d),wash_num(e){}void houseInfo(){cout住宅endl;buildingInfo();cout* 卧室数量sleep_numendl;cout* 浴室数量wash_numendl;}};class office:public building{private:int fire_num;int phone_num;public:office(int a,int b,int c,int d,int e):building(a,b,c),fire_num(d),phone_num(e){}void officeInfo(){cout办公室endl;buildingInfo();cout* 灭火器数量fire_numendl;cout* 电话数量phone_numendl;}};int main(){house h(10,5,300,2,1);office o(5,10,500,20,10);h.houseInfo();o.officeInfo();return 0;}6.3思考题1按照下图的类层次结构编写程序定义属于score的对象c1以及类teacher的对象t1分别输入每个数据成员的值后再显示出这些数据。 答#includeiostream#includecstring#includestringusing namespace std;class person{protected:string name;int id;public:person(string a,int b):name(a),id(b){}void personInfo(){cout* 姓名nameendl;cout* IDidendl;}};class teacher:public person{private:string degree;string dep;public:teacher(string a,int b,string c,string d):person(a,b),degree(c),dep(d){}void teacherInfo(){cout教师信息endl;personInfo();cout* 学历degreeendl;cout* 部门dependl;}};class student:public person{private:int old;int sno;public:student(string a,int b,int c,int d):person(a,b),old(c),sno(d){}void studentInfo(){cout学生信息endl;personInfo();cout* 年龄oldendl;cout* 学号snoendl;}};class stud{protected:string addr;string tel;public:stud(string a,string b):addr(a),tel(b){}void studInfo(){cout* 住址addrendl;cout* 电话telendl;}};class score:public student,public stud{private:double math;double eng;public:score(string a,int b,int c,int d,string e,string f,double s1,double s2):student(a,b,c,d),stud(e,f),math(s1),eng(s2){}void scoreInfo(){studentInfo();studInfo();cout* 数学成绩mathendl;cout* 英语成绩engendl;}};int main(){score a(张三,10086,20,2020123456,四川省成都市犀安路999号,15866668888,88.8,98.7);a.scoreInfo();teacher b(李四,10010,博士,计算机学院);b.teacherInfo();return 0;}本篇文章就到这里啦,祝你学习进步!
文章转载自: http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.vtbtje.cn.gov.cn.vtbtje.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.cqwb25.cn.gov.cn.cqwb25.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.yrdt.cn.gov.cn.yrdt.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.pmsl.cn.gov.cn.pmsl.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.xtdtt.cn.gov.cn.xtdtt.cn http://www.morning.rlwcs.cn.gov.cn.rlwcs.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.twdkt.cn.gov.cn.twdkt.cn