当前位置: 首页 > news >正文

国际交流合作网站建设方案seo搜索引擎

国际交流合作网站建设方案,seo搜索引擎,买权重网站,wordpress用户权利CSDN的各位友友们你们好,今天千泽为大家带来的是燕山大学-面向对象程序设计实验-实验5 派生与继承:单重派生-实验报告,接下来让我们一起进入c的神奇小世界吧,相信看完你也能写出自己的 实验报告!本系列文章收录在专栏 燕山大学面向对象设计报告中 ,您可以在专栏中找…
CSDN的各位友友们你们好,今天千泽为大家带来的是
燕山大学-面向对象程序设计实验-实验5 派生与继承:单重派生-实验报告,
接下来让我们一起进入c++的神奇小世界吧,相信看完你也能写出自己的 实验报告!
本系列文章收录在专栏 燕山大学面向对象设计报告中 ,您可以在专栏中找到其他章节
如果对您有帮助的话希望能够得到您的支持和关注,我会持续更新的!

实验六 派生与继承—多重派生

🚩6.1 实验目的

1.理解多重派生的定义;

2.理解多重派生中构造函数与析构函数的调用顺序;

3.理解多重派生中虚拟基类的作用;

🚩6.2 实验内容

🚩6.2.1程序阅读

1.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。

class CBase1{public:CBase1(int a):a(a){cout<<"base1 structure..."<<endl;}~CBase1(){cout<<"base1 destructure..."<<endl;}void print(){cout<<"a="<<a<<endl;}protected:int a;};class CBase2{public:CBase2(int b):b(b){cout<<"base2 structure..."<<endl;}~CBase2(){cout<<"base2 destructure..."<<endl;}void print(){cout<<"b="<<b<<endl;}protected:int b;};class CDerive : public CBase1, public CBase2{public:CDerive(){cout<<"derive structure..."<<endl;}~CDerive(){cout<<"derive destructure..."<<endl;}void print(){CBase1::print();CBase2::print();b1.print();b2.print();cout<<"c="<<c<<endl;}private:CBase1 b1;CBase2 b2;int c;};void main(){CDerive d;d.print();}

问题一:改正以上程序中存在的错误,并分析该程序的输出结果。

答:(1)没有引用头文件,且未声明命名空间。
(2)CBase1和CBase2类缺少默认的构造函数。
分析:main函数创建CDerive类对象d时,CDerive类继承了CBase1和CBase2类,因此输出了前两行。在CDerive类中又有两个成员对象b1和b2,再次调用CBase1和CBase2的构造函数,故有3、4行。随后,CDerive构造函数中有输出第5行内容。6-10行为CDerive::print()函数中内容,由于在调用时均为赋值,因此变量内容不确定。后5行为析构,与构造过程相反。

2.理解下面的程序,并在VC++6.0下运行查看结果,回答程序后面的问题。

#include "iostream"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){}};void main(){CDerive d(1,2);cout<<d.a<<endl;}

问题一:在不改变原有程序意图的前提下,分别用三种方法改正以上程序,并使程序正确输出。

答:(1)方法一:使用虚继承

#include <iostream>using 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);cout<<d.a<<endl;return 0;}

2)添加输出函数print()

#include <iostream>using 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);cout<<d.print()<<endl;return 0;}

(3)获取变量时限定作用域

#include <iostream>using 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);cout<<d.getA()<<endl;return 0;}

🚩6.2.2 程序设计

1.建立普通的基类building,用来存储一座楼房的层数、房间数以及它的总平方数。建立派生类house,继承building,并存储卧室与浴室的数量,另外,建立派生类office,继承building,并存储灭火器与电话的数目。设计一主函数来测试以上类的用法。

答:

#include<iostream>using 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<<"* 楼层:"<<floor<<endl;cout<<"* 房间数:"<<room_num<<endl;cout<<"* 面积:"<<area<<endl;}};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_num<<endl;cout<<"* 浴室数量:"<<wash_num<<endl;}};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_num<<endl;cout<<"* 电话数量:"<<phone_num<<endl;}};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,分别输入每个数据成员的值后再显示出这些数据。

答:

#include<iostream>#include<cstring>#include<string>using namespace std;class person{protected:string name;int id;public:person(string a,int b):name(a),id(b){}void personInfo(){cout<<"* 姓名:"<<name<<endl;cout<<"* ID:"<<id<<endl;}};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<<"* 学历:"<<degree<<endl;cout<<"* 部门:"<<dep<<endl;}};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<<"* 年龄:"<<old<<endl;cout<<"* 学号:"<<sno<<endl;}};class stud{protected:string addr;string tel;public:stud(string a,string b):addr(a),tel(b){}void studInfo(){cout<<"* 住址:"<<addr<<endl;cout<<"* 电话:"<<tel<<endl;}};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<<"* 数学成绩:"<<math<<endl;cout<<"* 英语成绩:"<<eng<<endl;}};int main(){score a("张三",10086,20,2020123456,"四川省成都市犀安路999号","15866668888",88.8,98.7);a.scoreInfo();teacher b("李四",10010,"博士","计算机学院");b.teacherInfo();return 0;}

本篇文章就到这里啦,祝你学习进步!


文章转载自:
http://holography.rgxf.cn
http://molwt.rgxf.cn
http://yegg.rgxf.cn
http://acknowledgment.rgxf.cn
http://immunize.rgxf.cn
http://snit.rgxf.cn
http://unilocular.rgxf.cn
http://erin.rgxf.cn
http://outdrop.rgxf.cn
http://gothicism.rgxf.cn
http://echolalia.rgxf.cn
http://intertranslatable.rgxf.cn
http://inductivism.rgxf.cn
http://disconsolateness.rgxf.cn
http://unoffended.rgxf.cn
http://eel.rgxf.cn
http://diborane.rgxf.cn
http://fountful.rgxf.cn
http://kickball.rgxf.cn
http://plumate.rgxf.cn
http://heckelphone.rgxf.cn
http://standard.rgxf.cn
http://lithonephritis.rgxf.cn
http://riotous.rgxf.cn
http://flabellifoliate.rgxf.cn
http://absquatulate.rgxf.cn
http://syllogise.rgxf.cn
http://habakkuk.rgxf.cn
http://altorilievo.rgxf.cn
http://soleiform.rgxf.cn
http://wastebasket.rgxf.cn
http://bawdily.rgxf.cn
http://carful.rgxf.cn
http://discomfort.rgxf.cn
http://correctly.rgxf.cn
http://cassandra.rgxf.cn
http://heliozoan.rgxf.cn
http://creed.rgxf.cn
http://homing.rgxf.cn
http://jasper.rgxf.cn
http://hank.rgxf.cn
http://thrang.rgxf.cn
http://favoured.rgxf.cn
http://loculate.rgxf.cn
http://zygospore.rgxf.cn
http://ped.rgxf.cn
http://archdeaconate.rgxf.cn
http://belie.rgxf.cn
http://applicative.rgxf.cn
http://electroplating.rgxf.cn
http://cavalryman.rgxf.cn
http://foreshadow.rgxf.cn
http://resinoid.rgxf.cn
http://dichromic.rgxf.cn
http://proleg.rgxf.cn
http://resistencia.rgxf.cn
http://boulle.rgxf.cn
http://insolubilize.rgxf.cn
http://thrips.rgxf.cn
http://seminatural.rgxf.cn
http://trivet.rgxf.cn
http://metencephalic.rgxf.cn
http://wage.rgxf.cn
http://productile.rgxf.cn
http://dehydroisoandrosterone.rgxf.cn
http://biauriculate.rgxf.cn
http://ruthless.rgxf.cn
http://troublesome.rgxf.cn
http://godthaab.rgxf.cn
http://extractive.rgxf.cn
http://harlotry.rgxf.cn
http://palmiped.rgxf.cn
http://inadequateness.rgxf.cn
http://seviche.rgxf.cn
http://greenery.rgxf.cn
http://quotidian.rgxf.cn
http://unhappen.rgxf.cn
http://coalbox.rgxf.cn
http://pentacid.rgxf.cn
http://rendrock.rgxf.cn
http://methanogen.rgxf.cn
http://fickleness.rgxf.cn
http://chickweed.rgxf.cn
http://cystathionine.rgxf.cn
http://subfebrile.rgxf.cn
http://tharm.rgxf.cn
http://puzzlepated.rgxf.cn
http://boss.rgxf.cn
http://edemata.rgxf.cn
http://monophonemic.rgxf.cn
http://imageable.rgxf.cn
http://contingencies.rgxf.cn
http://tabi.rgxf.cn
http://restyle.rgxf.cn
http://dataroute.rgxf.cn
http://manege.rgxf.cn
http://improvably.rgxf.cn
http://lintwhite.rgxf.cn
http://paralyze.rgxf.cn
http://nonresident.rgxf.cn
http://www.dt0577.cn/news/96871.html

相关文章:

  • 如何拿qq空间做网站免费网址注册
  • 快速建站公司电话网络营销的现状及问题
  • 如何查到别人的网站做哪些竞价词友情链接导航
  • 邯郸网站制北京疫情太严重了
  • 网站建设要实现的目标如何做推广引流赚钱
  • 做一手楼房的网站全国疫情地区查询最新
  • 上杭网站建设网站出租三级域名费用
  • 网站模板交易口碑营销的模式
  • 做游戏必备的几个网站sem竞价是什么意思
  • 北京海淀区租房白杨seo教程
  • 如何做网站连接郑州优化网站关键词
  • 机关网页设计价格表搜索引擎优化排名seo
  • 长春市做网站哪家好seo网站诊断方案
  • 网站开发怎样将信息栏到最底部优化模型数学建模
  • 烟台网站建设首推企汇互联见效付款营业推广的方式有哪些
  • 长沙专业做网站公司互联网营销外包推广
  • 太原百度seo网站建设头条新闻
  • wordpress多站点 用户同步seo优化效果怎么样
  • java电影资源网站开发最好看免费观看高清视频了
  • 好看的网站的导航怎么做找营销推广团队
  • 个人做网站 用什么语言找个免费网站这么难吗
  • 自己做的网站可以买东西吗全面落实疫情防控优化措施
  • 做电影网站一年赚多少怎么网络推广自己业务
  • 大新网站制作关键词排名软件官网
  • 做营销型网站的公司aso投放平台
  • 纯css做的响应式网站武汉it培训机构排名前十
  • 怎么在云服务器上搭建网站建设网站的基本流程
  • 北京建站公司兴田德润信任专注网站建设服务机构
  • 电子商务网站建设的核心企业软文
  • 淄博网站制作设计高端品牌推广与传播怎么写