《继承和派生》PPT课件.ppt

上传人:牧羊曲112 文档编号:5590066 上传时间:2023-07-31 格式:PPT 页数:18 大小:202KB
返回 下载 相关 举报
《继承和派生》PPT课件.ppt_第1页
第1页 / 共18页
《继承和派生》PPT课件.ppt_第2页
第2页 / 共18页
《继承和派生》PPT课件.ppt_第3页
第3页 / 共18页
《继承和派生》PPT课件.ppt_第4页
第4页 / 共18页
《继承和派生》PPT课件.ppt_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《《继承和派生》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《继承和派生》PPT课件.ppt(18页珍藏版)》请在三一办公上搜索。

1、第11章 继承和派生,11.1 继承 一个类继承另一个类的过程成为派生一个类,派生出来的类称为派生类或子类。而被继承的类称为基类或父类,类的派生过程可以无限进行下去。11.1.1 单一继承 基类只有一个。格式:class private public private:;public:;protected:;,说明:1 继承可以部分继承,也可以在派生类中增加新的成员。2派生分两类:私有和公有。11.1.2公有派生 公有派生时,访问基类成员的权限在派生类中保持不变。即从类外可以访问从基类中派生下来的公有成员。例11-1:定义一个B类,从B类公有派生D类,并在D类中在增加一个私有数据成员Z和三个公有

2、函数。,程序:class B int x,y;public:void Setx(int a)x=a;void Sety(int b)y=b;int Getx()return x;int Gety()return y;,class D:public B int z;public:void Setz(int c)z=c;int Getz()return z;int Sum()return(Getx()+Gety()+Getz();#include iostream.h void main()D d;d.Setx(1);/由于是公有派生,类外可访问,直接访问 d.Sety(2);/继承后可以用d调用

3、b类中的成员 d.Setz(3);coutx=d.Getx()endl;couty=d.Gety()endl;coutz=d.Getz()endl;coutsum=d.Sum()endl;说明:派生类D的定义与主函数放在一个程序。赋值与使用成员均在类内部进行。对类中的成员的访问均通过函数成员进行。派生出来的公有成员仍通过派生类的对象名访问。在派生类中不能访问基类的私有成员。,11.1.3 私有派生 基类成员在派生类中进行封装说明,在派生类外部(如函数)均不可见,而使用的是封装后的函数名(成员名)。例11-2:从B类私有派生D类,然后进行输入和输出。class B int x,y;public:

4、void Setx(int a)x=a;void Sety(int b)y=b;int Getx()return x;int Gety()return y;,#include iostream.h class D:private B/私有派生 int z;public:void Setz(int c)z=c;void Setbase(int a,int b)Setx(a);Sety(b);int Getbx()return Getx();/私有派生时,基类中的公有成员不能直接被访问(Getx()int Getby()return Gety();int Getz()return z;int Su

5、m()return(z+Getx()+Gety();,void main()D d;d.Setbase(1,2);/私有派生时,基类中的公有成员不能直接访问 d.Setz(3);coutx=d.Getbx()endl;couty=d.Getby()endl;coutz=d.Getz()endl;coutsum=d.Sum()endl;,11.1.4 保护成员 对于派生类,它是公有的;对于外部程序,它是私有的,即在派生过程中,保护成员不用封装。例11-3class B protected:int x,y;public:void Setx(int a)x=a;void Sety(int b)y=b

6、;int Getx()return x;int Gety()return y;,#include iostream.h class D:private B int z;public:void Setz(int c)z=c;void Setbase(int a,int b)x=a;/对于派生类,仍是公有 y=b;int Getbx()return Getx();int Getby()return Gety();int Getz()return z;int Sum()return(z+x+y);,void main()D d;d.Setbase(1,2);/对于外部程序,它是私有的,成员不能直接访

7、问 d.Setz(3);coutx=d.Getbx()endl;couty=d.Getby()endl;coutz=d.Getz()endl;coutsum=d.Sum()endl;,11.1.2 多重继承利用多个基类派生的类。格式:class private,private,public,public private:;public:;protected:;,例:11-4 class A protected:/保护成员 int w;public:void Setw(int a)w=a;int Getw()return w;,class B protected:int x,y;public:v

8、oid Setx(int a)x=a;void Sety(int b)y=b;int Getx()return x;int Gety()return y;,class D:public A,B int z;public:void Setz(int c)z=c;void Setbase(int a,int b,int c)x=a;/x,y,w仍是公有 y=b;w=c;,int Getbx()return x;int Getby()return y;int Getbw()return w;int Getz()return z;int Sum()return(z+x+y+w);,#include iostream.hvoid main()D d;d.Setbase(1,2,3);d.Setz(4);coutx=d.Getbx()endl;couty=d.Getby()endl;coutz=d.Getbw()endl;coutz=d.Getz()endl;coutsum=d.Sum()endl;,输出结果是:程序说明:从类 A,B中派生出了类D 仅继承私有成员(数据成员)其中 setw()不该封装,直接使用即可.,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号