版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、10实验三 继承和派生类实验目的和要求1 理解类的继承的概念,能够定义和使用类的继承关系。2 掌握派生类的声明与定义方法。3 熟悉公有派生和私有派生的访问特性。4 学习虚基类在解决二义性问题中的作用。实验内容验证自己分析的结1. 先阅读下面的程序, 分析程序运行的结果,然后再上机运行程序, 果是否正确。(1) #include class A public: A() coutA:A() called.n;A()coutA:A() called.n; ; class B:public A public: B(int i)coutB:B() called.n; buf=new chari;B()d
2、elete buf; coutB:B() called.n; private: char *buf;void main()B b(10);(2)#includeclass Apublic:A(int a,int b):x(a),y(b)coutA constructor.endl;void Add(int a,int b)x+=a;y+=b;void display()cout(x,y)endl;A()coutdestructor A.endl;private:i nt x,y;class B:private Aprivate:i nt i,j;A Aobj;public:B(int a,in
3、t b,int c,int d):A(a,b),i(c),j(d),Aobj(1,1) coutB constructor.endl;void Add(int x1,int y1,int x2,int y2)A:Add(x1,y1);i+=x2;j+=y2;void display()A:display();();cout(i,j)endl;B()coutdestructor B.endl; ; void main()B b(1,2,3,4);();( 1,3,5,7);();(3) #include class A public: A(int a):x(a) coutA constructo
4、r.xendl;i nt f()return +x;A()coutdestructor A.endl; private: i nt x;class B:public virtual A private: i nt y;A Aobj; public:B(int a,int b,int c):A(a),y(c),Aobj(c)coutB constructor.yendl;i nt f()A:f();(); return +y;void display() coutA:f()t()tf()endl;B()coutdestructor B.endl;class C:public Bpublic:C(
5、int a,int b,int c):B(a,b,c),A(0)coutC constructor.endl;class D:public C,virtual public Apublic:D(int a,int b,int c):C(a,b,c),A(c)coutD constructor.endl;D()coutdestructor D.endl; void main()Dd(7,8,9);();();(4)#includeclass Base1public:Base1()coutclass Base1!endl;class Base2public:Base2()coutclass Bas
6、e2!endl;class Level1:public Base2,virtual public Base1public:Level1()coutclass Level1!endl;class Level2:public Base2,virtual public Base1public:Level2()coutclass Level2!endl;class TopLevel:public Level1,virtual public Level2 public:TopLevel()coutclass TopLevel!endl;void main()TopLevel obj;2. 调试下列程序,
7、并对程序进行修改后再调试,指出调试中的出错原因。#include class Apublic:void seta(int i) a=i; i nt geta() return a; public:i nt a;class B:public Apublic:void setb(int i) b=i; i nt getb() return b; void show() coutA:a=ai* A .-. des iLriuic tu r A .-.Press ani/ key to conLnue1. (4)r ha t la&SC 1(133 L Idya c lass c lassPressB
8、A&elfVLuel2!Leu ell!TopLeue1!any key to continue2.(1) 主函数中语句2、语句5、语句7出现错误。因为B采 用私有继承,所以A中的公有函数在B中是变为私有函数,主函数中不能访问 B 中的私有函数(2) 主函数中语句 2、语句 5、语句 7 出现错误。因为 B 采 用保护继承, 所以 A 中的公有函数在 B 中是变为保护函数, 主函 数中不能访问 B 中的保护函数。(3) 主函数中语句 9、语句 5 出现错误。因为 B 采用公有 继承,所以 B 中和主函数中不能访问 A 中的私有函数。(4) 主函数中语句 5 出现错误。因为 B 采用公有继承,所
9、 以 A 中保护成员在 B 中仍为保护成员, B 中可以访问 A 中的保护 成员,但不能通过类的对象访问类的保护成员。3.(1)#include class Myarrayprotected:int *alist;.endl;Myarray() .endl;void sort()int i,j,k,temp;for(i=0;ilength-1;i+)k=i;for(j=i+1;jlength;j+)if(alistjalistk)k=j;temp=alisti;alisti=alistk; alistk=temp;void main()SortArray B(4);();();endl;cou
10、tendl 现在将数组内成员进行从小到大排序:();();(3)#include class Myarray protected: int *alist; .endl;Myarray() .endl; void turn()int i,t,j=length-1;for(i=0;ilength/2;i+,j-)t=alisti; alisti=alistj; alistj=t; ;void main()ReArray B(4);();();endl;coutendl 现在将数组内成员进行前后逆转: ();();(4)#include class Myarrayprotected:int *ali
11、st; .endl;Myarray() .endl;float getaver()float aver;int sum=0,i;for(i=0;ilength;i+) sum+=alisti; aver=(float)sum/length; return aver;void main()AverArray B(3);();();coutendl 数组内成员的平均值为: cout() endl;#include class Myarrayprotected:int *alist; .endl;Myarray() .endl;void get_SortArray()int i,j,k,temp;f
12、or(i=0;ilength-1;i+)k=i;for(j=i+1;jlength;j+) if(alistjalistk) k=j;temp=alisti;alisti=alistk; alistk=temp;class ReArray: virtual public Myarray.endl;void get_ReArray()int i,t,j=length-1;for(i=0;ilength/2;i+,j-)t=alisti;alisti=alistj;alistj=t;.endl;class AverArray:virtual public Myarray float get_Ave
13、rArray()float aver;int sum=0,i;for(i=0;ilength;i+) sum+=alisti; aver=(float)sum/length; return aver;class NewArray:public SortArray,public ReArray,public AverArray public:NewArray(int n):SortArray(n),ReArray(n),AverArray(n),Myarray(n) coutNewArray 初始化 .endl;void fun() coutendl 数组成员的平均值为: ; coutAverArray:get_AverArray() endl;coutendl 现在将数组成员进行前后逆转: endl; ReArray:get_ReArray();Myarray:Display();coutendl 现在将数组成员进行从小到大排序: endl; SortArray:get_SortArray(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026孤独症康复面试题及答案
- 2026海淀工商面试题目及答案
- 2026基层矛盾面试题及答案
- 2026教师素质综合素养面试题及答案
- 辽宁辽南协作体2025-2026学年高一下学期期末英语试题(含答案)
- 保险AI在客户服务中的个性化响应
- 人工智能在金融风险评估中的应用-第77篇
- 巴中市2026年第六批就业见习岗位的考试模拟试题及答案详解
- 2026年佛山市顺德区住房和城乡建设局人员招聘笔试参考题库及答案详解
- 2026西安建筑科技大学附属中学招聘考试参考题库及答案详解
- 北京市东城区2025-2026学年高一第二学期期末英语试题(含答案)
- 院内病人转运安全管理
- 济南水务集团招聘笔试真题2024
- 骨科围术期血糖管理
- 业主委员会工作实务手册
- 第一单元 分数加减法单元测试(含答案)北师大版五年级下册数学
- 社区服务项目合伙人协议书
- 污水处理设施维护计划
- GB/T 11263-2024热轧H型钢和剖分T型钢
- 配电箱巡查记录表
- GB/T 24067-2024温室气体产品碳足迹量化要求和指南
评论
0/150
提交评论