- 1、本文档共63页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
★第13章 继承和派生.pptx
第13章 继承和派生;13.1 继承;举例:见图13-1,图中的箭头是从派生类指向基类。
在校人员类:描述在校人员的共性信息(如姓名、年龄、身高和性别等)。
学生类:单一继承“在校人员类”,并增加描述学生的个性信息(如学号、所学专业和课程等)。;单一继承;公有派生;public:
int z;
A(int a,int b,int c){ x=a;y=b;z=c; }
void Setx(int a){ x=a; }
void Sety(int a){ y=a; }
int Getx( ){ return x; }
int Gety( ){ return y; }
void ShowB( )
{coutx=x\ty=y\tz=z\n;}
};
class B:public A //公有继承基类A,派生类B
{ int Length,Width;
public:
B(int a,int b,int c,int d,int e): A(a,b,c)
{ Length=d;Width=e; }; void Show( )
{ coutLength=Length\tWidth=Width\n;
coutx=Getx( )\ty=y\tz=z\n;
}
int Sum( ){ return Getx( )+y+z+Length+Width; }
};
?
void main(void)
{ B b1(1,2,3,4,5);
b1.ShowB( );
b1.Show( );
coutSum=b1.Sum( );
cout\ny=b1.Gety( );
cout\tz=b1.z\n;
};私有派生;public:
int z;
A(int a,int b,int c){ x=a;y=b;z=c; }
void Setx(int a){ x=a; }
int Getx( ){ return x; }
int Gety( ){ return y; }
};
?
class B:private A //私有继承基类A,派生类B
{ int Length,Width;
public:
B(int a,int b,int c,int d,int e) : A(a,b,c)
{ Length=d;Width=e; }
void Show( )
{ coutLength=Length\tWidth=Width\n;; coutx=Getx( )\ty=y\tz=z\n;
}
int Sum(void)
{ return Getx( )+y+z+Length+Width; }
};
?
void main(void)
{ B b1(1,2,3,4,5);
b1.Show( );
coutSum=b1.Sum( )\n;
};实际编程中,公有派生用得最普遍,私有派生用得较少,而保护派生极少使用,这里不作介绍。
在派生类内外,对继承基类成员的访问权限,如表11-1所示。
表13.1 公有和私有派生;抽象类;多重继承;class A //描述圆:(x,y)为圆心,r为半径
{ float x,y,r;
public:
A(float a,float b,float c){ x=a;y=b;r=c; }
float AccessX( ){ return x; }
float AccessY( ){ return y; }
float AccessR( ){ return r; }
float Area( ){ return r*r*3.14159f; }
};
class B
{ float High;
public:
B(float a){ High=a; }
float AccessHigh( ){ return High; }
};?;class C:public A,private B //描述圆柱体
{ float Volume;//圆柱体的体积
public:
C(float a,float b,float c,float d):A(a,b,c),B(d)
{ Volume=Area( )*AccessHigh( ); }
float GetVolume( ){ return Volume; }
};
?
void main(void)
{ A a(6,8,9); B b=
文档评论(0)