ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:25.22KB ,
资源ID:7300426      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7300426.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(《C++程序设计教程 第2版》教学素材PPT讲稿教学课件第12章 继承和派生例子.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

《C++程序设计教程 第2版》教学素材PPT讲稿教学课件第12章 继承和派生例子.docx

1、C+程序设计教程 第2版教学素材PPT讲稿教学课件第12章 继承和派生例子例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 Cir

2、cle: public Point / 定义“圆”类,公有继承 int r; / “圆”的半径 public: 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( ); / D x1 += x_offset; y1 += y_offset;

3、Setxy(x1, y1); / E void ShowCircle( ) ShowPoint( ); / F cout 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类多重派生出“圆

4、”类Circle。#include #define PI 3.14159class Point protected: /A 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 Radius protected: /B int r;public: Radius(int ra=0) r = ra; void

5、Setr(int ra) r = ra; int Getr( ) return r; ;class Circle : public Point, 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( ); cou

6、tRadius: rt; coutArea: Area( )endl; ;void main( ) Circle c(1, 1, 1); c.ShowCircle( ); 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 多重继承中基类构造函数和析

7、构函数的调用顺序#include class Base1protected: int data1;public: Base1(int a=0) data1 = a; coutBase 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 B

8、ase2 /A int d;public: Derived(int x, int y, int z):Base1(x), Base2(y) /B d=z; coutDerived Constructorn; Derived( ) coutDerived Destructorn; void Show( ) coutdata1,data2,dendl; ;void main( ) Derived c(1, 2, 3); c.Show( );程序的运行结果是:Base Constructor1Base Constructor2Derived Constructor1, 2, 3Derived Des

9、tructorBase Destructor2Base Destructor1返回ppt讲稿 例12.4 对象成员构造函数和析构函数的调用顺序#include class Base1protected: 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

10、Constructor2n; Base2( ) coutdata2, Base Destructor2n; ;class Derived:public Base1, public Base2 /A int d; Base1 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,den

11、dl; ;void main( ) Derived c(1, 2, 3); c.Show( );程序的运行结果是:1, Base Constructor12, Base Constructor23, Base Constructor1 /构造对象成员b1时的输出 4, Base Constructor1 /构造对象成员b2时的输出 Derived Constructor1, 2, 3Derived Destructor4, Base Destructor1 /析构对象成员b2时的输出 3, Base Destructor1 /析构对象成员b1时的输出 2, Base Destructor21,

12、 Base Destructor1返回ppt讲稿 例12.7 多层继承中的冲突,注意本例的继承关系如右下图所示:#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(i

13、nt b) y=b; int Gety( ) return y; ;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类中增加成

14、员函数: void ShowA( ) cout x= A:x n; void ShowB( ) 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类对象有几个数据成

15、员。 int x;public: void SetAx(int a) A:x = a; /访问的是基类A的x void SetBx(int a) x = a; /访问的是派生类B的x void 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( ) cout

16、A;class Bpublic: B( ) coutB; B( ) coutB;class Cpublic: 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 Point int x, y;public: Point

17、(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 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.G

18、etx( ); y1=p1.Gety( ); /访问对象成员p1的成员 x2=p2.Getx( ); y2=p2.Gety( ); /访问对象成员p2的成员 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

19、=1.41421注意在本程序中,Point类和Line类不是继承关系,只是Point类的两个对象,是Line类的对象成员,访问对象成员的成员,与访问一般对象的成员遵循同样的规则。返回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 Ge

20、ty( ) return y; void Show( ) coutpoint(x,y)n; ;class Line : public Point /公有继承 protected : int x1, y1; public: Line(int a, int b, int c, int d) : Point(a, b) /调用基类构造函数 x1=c; y1=d; double Length( ) return sqrt(x-x1)*(x-x1)+(y-y1)*(y-y1); / A 直接访问基类保护成员x、y void Show( ) Point:Show( ); /访问基类成员函数Show( )

21、coutpoint(x1,y1 )n; coutLength=Length( )endl; ;void main( ) Line line(0, 0, 1, 1); line.Show( );返回ppt讲稿 例12.14 赋值兼容规则#include class Pointprotected: int x, y; / 保护成员public: Point(int a=0, int b=0) x=a; y=b; void Show( ) coutpoint(x,y)n; ;class Line : public Point / 公有继承protected: int x1, y1;public: Line(int a, int b, int c, int d): Point(a, b) /调用基类构造函数 x1=c; y1=d; ;void main( ) Line line(2, 2, 6, 6); Point p; p = line; / A p.Show( );程序运行结果是: Point(2, 2)返回ppt讲稿

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1