《C++程序设计教程 第2版》教学素材PPT讲稿(教学课件)第12章 继承和派生(例子)_第1页
《C++程序设计教程 第2版》教学素材PPT讲稿(教学课件)第12章 继承和派生(例子)_第2页
《C++程序设计教程 第2版》教学素材PPT讲稿(教学课件)第12章 继承和派生(例子)_第3页
《C++程序设计教程 第2版》教学素材PPT讲稿(教学课件)第12章 继承和派生(例子)_第4页
《C++程序设计教程 第2版》教学素材PPT讲稿(教学课件)第12章 继承和派生(例子)_第5页
已阅读5页,还剩16页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、例12.1 先定义“点”类Point,再由“点”类派生出“圆”类Circle。#include #define PI 3.14159class Point / 定义“点”类int x, y;public:Point(int a=0, int b=0)x=a; y=b;void ShowPoint( )coutPoint:(x,y)n;int Getx( ) return x; int Gety( )return y;void Setxy(int a, int b)x=a; y=b;class Circle: public Point / 定义“圆”类,公有继承int r; / “圆”的半径 p

2、ublic:Circle(int x, int y, int ra) : Point(x, y) / B r = ra; void Setr(int ra)r = ra; double Area( ) /求圆的面积return PI*r*r;void Move(int x_offset, int y_offset) /将圆心坐标平移int x1=Getx( ); /存取基类的私有成员int y1=Gety( ); / Dx1 += x_offset; y1 += y_offset;Setxy(x1, y1); / Evoid ShowCircle( )ShowPoint( ); / Fcout

3、 Radius: rt;coutArea: Area( )endl; /G;void main( )Circle c(1, 1, 1);c.ShowCircle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5); /重新置圆心坐标 c.Setr(2); /重新置半径值 c.ShowCircle( );回ppt讲稿 例12.2 先定义“点”类Point和“半径”类Radius,再由Point类和Radius类多重派生出“圆”类Circle。#include #define PI 3.14159class Point protected: /A int x

4、, y;public:Point(int a=0, int b=0) x=a; y=b; void ShowPoint( ) coutPoint:(x,y)n; int Getx( ) return x; int Gety( ) return y; void Setxy(int a, int b) x=a; y=b; ;class Radius protected: /Bint r;public:Radius(int ra=0) r = ra; void Setr(int ra) r = ra; int Getr( ) return r; ;class Circle : public Poin

5、t, public Radius public:Circle(int x, int y, int ra) : Point(x, y), Radius(ra) /D double Area( ) return PI*r*r; /直接访问基类的保护成员 void Move(int x_offset, int y_offset) x += x_offset; y += y_offset; void ShowCircle( ) ShowPoint( );coutRadius: rt;coutArea: Area( )endl;void main( )Circle c(1, 1, 1);c.ShowCi

6、rcle( );c.Move(1, 2);c.ShowCircle( );c.Setxy(4, 5);c.Setr(2);c.ShowCircle( );程序的运行结果为:Point:(1, 1)Radius: 1 Area: 3.14159Point:(2, 3)Radius: 1 Area: 3.14159Point:(4, 5)Radius: 2 Area: 12.5664返回ppt讲稿 例12.3 多重继承中基类构造函数和析构函数的调用顺序#include class Base1protected: int data1;public:Base1(int a=0)data1 = a; c

7、outBase Constructor1n;Base1( )coutBase Destructor1n; ;class Base2protected: int data2;public:Base2(int a=0)data2 = a; coutBase Constructor2n;Base2( )coutBase Destructor2n; ;class Derived: public Base1, public Base2 /Aint d;public:Derived(int x, int y, int z):Base1(x), Base2(y) /B d=z; coutDerived Co

8、nstructorn;Derived( ) coutDerived Destructorn; void Show( ) coutdata1,data2,dendl; ;void main( ) Derived c(1, 2, 3);c.Show( );程序的运行结果是:Base Constructor1Base Constructor2Derived Constructor1, 2, 3Derived DestructorBase Destructor2Base Destructor1返回ppt讲稿 例12.4 对象成员构造函数和析构函数的调用顺序#include class Base1pro

9、tected: int data1; public:Base1(int a=8)data1 = a; coutdata1, Base Constructor1n;Base1( )coutdata1, Base Destructor1n;class Base2protected: int data2;public:Base2(int a=9)data2 = a; coutdata2, Base Constructor2n;Base2( )coutdata2, Base Destructor2n;class Derived:public Base1, public Base2 /A int d;B

10、ase1 b1, b2; /Bpublic:Derived(int x, int y, int z) : Base1(x), Base2(y), b1(x+y), b2(x+z) /C d=z; coutDerived Constructorn;Derived( ) coutDerived Destructorn;void Show( )coutdata1,data2,dendl;void main( )Derived c(1, 2, 3);c.Show( );程序的运行结果是:1, Base Constructor12, Base Constructor23, Base Constructo

11、r1 /构造对象成员b1时的输出 4, Base Constructor1 /构造对象成员b2时的输出 Derived Constructor1, 2, 3Derived Destructor4, Base Destructor1 /析构对象成员b2时的输出 3, Base Destructor1 /析构对象成员b1时的输出 2, Base Destructor21, Base Destructor1返回ppt讲稿 例12.7 多层继承中的冲突,注意本例的继承关系如右下图所示:C y, SetAx( ),SetBx( ),Sety( ),Gety( ) A x, Show( ) B x, Sh

12、ow( ) D z, Setz( ),Getz( ) #include class Aprotected:int x;public:void Show( ) cout x= x n ; ;class Bprotected:int x;public:void Show( ) cout x= x n ; ;class C: public A, public B /公有继承 A、B 类 int y;public:void SetAx(int a) A:x=a; void SetBx(int a) B:x=a; void Sety(int b) y=b; int Gety( ) return y; ;

13、class D: public C /公有继承 C 类 int z;public:void Setz(int a) z=a; int Getz( ) return z; ;void main(void)D d;d.SetAx(10); d.SetBx(20); d.Sety(30);d.Setz(40);coutA; d.C:A:Show( ); /E 报错 coutB; d.C:B:Show( ); /F 报错 cout y= d.Gety( ) n;cout z= d.Getz( ) n;解决1:在C类中增加成员函数:void ShowA( ) cout x= A:x n; void Sh

14、owB( ) cout x= B:x n; 再将E行和F行改写成:d.ShowA( ); 和d.ShowB( ); 即可。解决2:把E行和F行改写成:d.A:Show( ); 和d.B:Show( ); 即可。返回ppt讲稿 例12.8 支配规则示例 #include class Aprotected: int x;public:void Set(int a) x=a; void Show( ) cout x= x n ; ;class B : public Aprotected: 问题:类B类对象有几个数据成员。 int x;public:void SetAx(int a) A:x = a;

15、 /访问的是基类A的xvoid SetBx(int a) x = a; /访问的是派生类B的xvoid Show( ) coutx= x endl; ;void main(void)B b;b.SetAx(1);b.SetBx(2);b.A:Show( ); /访问的是基类A的Show( )b.Show( ); /访问的是派生类B的Show( )返回ppt讲稿 例12.11 虚基类和非虚基类构造函数的调用。#include class Apublic:A( ) coutA;A( ) coutA;class Bpublic:B( ) coutB;B( ) coutB;class Cpublic:

16、C( ) coutC;C( ) coutC;class Dpublic:D( ) coutD;D( ) coutD;class E: virtual public B, public A, public D, virtual public C ;int main( )E c;return 0;程序的运行结果是:BCADDACB返回ppt讲稿 例12.12 访问对象成员的成员#include #include class Pointint x, y;public:Point(int a=0, int b=0) x=a; y=b;void Setx(int a)x=a;void Sety(int

17、a)y=a;int Getx( ) return x;int Gety( ) return y;void Show( )coutpoint(x,y)n;class Line Point p1, p2; /对象成员public:Line(int x1, int y1, int x2, int y2): p1(x1, y1), p2(x2, y2)/调用对象成员构造函数 double Length( )int x1, y1, x2, y2;x1=p1.Getx( ); y1=p1.Gety( ); /访问对象成员p1的成员x2=p2.Getx( ); y2=p2.Gety( ); /访问对象成员p

18、2的成员return sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);void Show( )p1.Show( ); /访问对象成员p1的成员p2.Show( ); /访问对象成员p2的成员coutLength=Length( )endl;void main( )Line line(0, 0, 1, 1);line.Show( );本程序的运行结果是:point(0, 0)point(1, 1)Length=1.41421注意在本程序中,Point类和Line类不是继承关系,只是Point类的两个对象,是Line类的对象成员,访问对象成员的成员,与访问一般对象的成员遵循

19、同样的规则。返回ppt讲稿 例12.13 访问基类成员#include #include class Pointprotected:int x, y; /定义x、y为保护成员,以使在公有派生类中可直接访问它们public:Point(int a=0, int b=0) x=a; y=b;void Setx(int a)x=a;void Sety(int a)y=a;int Getx( ) return x; int Gety( ) return y; void Show( ) coutpoint(x,y)n; ;class Line : public Point /公有继承 protected : int x1, y1; public:Li

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论