第7章 继承.ppt

上传人:周芸 文档编号:388637 上传时间:2018-10-12 格式:PPT 页数:123 大小:1.34MB
下载 相关 举报
第7章 继承.ppt_第1页
第1页 / 共123页
第7章 继承.ppt_第2页
第2页 / 共123页
第7章 继承.ppt_第3页
第3页 / 共123页
第7章 继承.ppt_第4页
第4页 / 共123页
第7章 继承.ppt_第5页
第5页 / 共123页
亲,该文档总共123页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、第7章 继承,7.1 类之间的关系,7.2 基类和派生类,7.3 基类的初始化,7.4 继承的应用实例,7.5 多继承,小结,7.1 类之间的关系,has-A,uses-A 和 is-A has-A 包含关系,用以描述一个类由多个“部件类”构成。实现has-A关系用类成员表示,即一个类中的数据成员是另一种已经定义的类。 uses-A 一个类部分地使用另一个类。通过类之间成员函数的相互联系,定义友员或对象参数传递实现。 is-A 机制称为“继承”。关系具有传递性,不具有对称性。,7.1 类之间的关系,不具有对称性 不是所有植物都属于蕨类,传递性 高等植物、蕨类植物、芒萁都是植物,具有植物的共同特

2、征,植物,7.1 类之间的关系,继承 是类之间定义的一种重要关系一个 B 类继承A类,或称从类 A 派生类 B类 A 称为基类(父类),类 B 称为派生类(子类),A,C3,B1,B2 的基类,A 的派生类 C1,C2,C3 的基类,A 的派生类(单继承) C3的基类,B1,B2 的派生类(多继承),B1 的派生类,类继承关系的语法形式class 派生类名 : 基类名表数据成员和成员函数声明;,7.2 基类和派生类,类继承关系的语法形式class 派生类名 : 基类名表数据成员和成员函数声明;,7.2 基类和派生类,基类名表 构成访问控制 基类名1, 访问控制 基类名2 , , 访问控制 基类

3、名n,类继承关系的语法形式class 派生类名 : 基类名表数据成员和成员函数声明;,7.2 基类和派生类,基类名表 构成访问控制 基类名1, 访问控制 基类名2 , , 访问控制 基类名n,访问控制 表示派生类对基类的继承方式,使用关键字:public 公有继承private 私有继承protected 保护继承,派生类对基类成员的使用,与继承访问控制和基类中成员性质有关 公有继承 基类的公有成员派生类的公有成员基类的保护成员派生类的保护成员 私有继承 基类的公有成员和保护成员派生类的私有成员 保护继承 基类的公有成员和保护成员派生类的保护成员 不论种方式继承基类,派生类都不能直接使用基类的

4、私有成员,7.2.1 访问控制,7.2.1 访问控制,7.2.1 访问控制,1.公有继承,#include class A public :void get_XY() cout x y ; void put_XY() cout h ; int get_V() return v ; void make_V() make_S(); v = get_S() * h ; / 使用基类成员函数protected: int h, v; ;,例7-1 公有继承的测试,7.2.1 访问控制,#include class A public :void get_XY() cout x y ; void put_X

5、Y() cout h ; int get_V() return v ; void make_V() make_S(); v = get_S() * h ; / 使用基类成员函数protected: int h, v; ;,7.2.1 访问控制,例7-1 公有继承的测试,#include class A public :void get_XY() cout x y ; void put_XY() cout h ; int get_V() return v ; void make_V() make_S(); v = get_S() * h ; / 使用基类成员函数protected: int h,

6、 v; ;,保护数据成员 在类层次中可见,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;cout “V

7、 = “ objC.get_V() endl ; ,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;co

8、ut “V = “ objC.get_V() endl ; ,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V(

9、) ;cout “V = “ objC.get_V() endl ; ,x y,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();obj

10、C.make_V() ;cout “V = “ objC.get_V() endl ; ,x y,s,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.

11、get_H();objC.make_V() ;cout “V = “ objC.get_V() endl ; ,对 objA 的 数据成员操作,x y,s,h v,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is obj

12、ect_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;cout “V = “ objC.get_V() endl ; ,调用基类A成员函数 对 objB 的数据成员操作,x y,s,h v,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “

13、S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;cout “V = “ objC.get_V() endl ; ,调用派生类B成员函数 对 objB 的数据成员操作,x y,s,h v,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B

14、:n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;cout “V = “ objC.get_V() endl ; ,调用基类A成员函数 对 objC 的数据成员操作,x y,s,h v,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C objC ;cout “It is object_A :n“ ;objA.get_XY

15、() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;cout “V = “ objC.get_V() endl ; ,调用派生类C成员函数 对 objC 的数据成员操作,x y,s,h v,7.2.1 访问控制,例7-1 公有继承的测试,void main() A objA ;B objB ;C ob

16、jC ;cout “It is object_A :n“ ;objA.get_XY() ;objA.put_XY() ;cout “It is object_B :n“ ;objB.get_XY() ;objB.make_S() ;cout “S = “ objB.get_S() endl ;cout “It is object_C :n“ ;objC.get_XY() ;objC.get_H();objC.make_V() ;cout “V = “ objC.get_V() endl ; ,x y,s,h v,7.2.1 访问控制,例7-1 公有继承的测试,7.2.1 访问控制,2.私有继承

17、,#include class A public :void get_XY() cout x y ; void put_XY() cout “x = “ x “, y = “ y n ; protected: int x, y ; ; class B : private A public :int get_S() return s ; void make_S() get_XY(); s = x * y ; private: int s ; ; void main() B objB ; cout “It is object_B :n“ ;objB.make_S() ;cout “S = “ ob

18、jB.get_S() endl ;,例7-2 私有继承的测试,7.2.1 访问控制,#include class A public :void get_XY() cout x y ; void put_XY() cout “x = “ x “, y = “ y n ; protected: int x, y ; ; class B : private A public :int get_S() return s ; void make_S() get_XY(); s = x * y ; private: int s ; ; void main() B objB ; cout “It is ob

19、ject_B :n“ ;objB.make_S() ;cout “S = “ objB.get_S() endl ;,调用基类成员函数,7.2.1 访问控制,例7-2 私有继承的测试,#include class A public :void get_XY() cout x y ; void put_XY() cout “x = “ x “, y = “ y n ; protected: int x, y ; ; class B : private A public :int get_S() return s ; void make_S() get_XY(); s = x * y ; priv

20、ate: int s ; ; void main() B objB ; cout “It is object_B :n“ ;objB.make_S() ;cout “S = “ objB.get_S() endl ;,访问私有数据成员,7.2.1 访问控制,例7-2 私有继承的测试,#include class A public :void get_XY() cout x y ; void put_XY() cout “x = “ x “, y = “ y n ; protected: int x, y ; ; class B : private A public :int get_S() r

21、eturn s ; void make_S() get_XY(); s = x * y ; private: int s ; ; void main() B objB ; cout “It is object_B :n“ ;objB.make_S() ;cout “S = “ objB.get_S() endl ;,7.2.1 访问控制,例7-2 私有继承的测试,#include class A public: A() x=1; int out() return x ; void addX() x+; private: int x ; ; class B : public A public:

22、B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A a ;B b ;b.addX() ; b.addY() ;cout “a.x=“ a.out() endl ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,例7-3 私有数据成员的测试,基类的私有数据成员 不能在派生类中直接访问 但派生类对象建立私有数据空间,7.2.1 访问控制,#include class A public: A() x=1; int out() ret

23、urn x ; void addX() x+; private: int x ; ; class B : public A public: B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,7.2.1 访问控制,例7-3 私有数据成员的测试,#include class

24、 A public: A() x=1; int out() return x ; void addX() x+; private: int x ; ; class B : public A public: B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,调用构造函数

25、对数据成员赋值,7.2.1 访问控制,例7-3 私有数据成员的测试,#include class A public: A() x=1; int out() return x ; void addX() x+; private: int x ; ; class B : public A public: B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A

26、:out() endl ;cout “b.y=“ b.out() endl ; ,创建派生类对象 调用基类 派生类构造函数 b.x = 1 b.y = 1,7.2.1 访问控制,例7-3 私有数据成员的测试,#include class A public: A() x=1; int out() return x ; void addX() x+; private: int x ; ; class B : public A public: B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A

27、a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,调用基类公有成员函数 b.x +,7.2.1 访问控制,例7-3 私有数据成员的测试,#include class A public: A() x=1; int out() return x ; void addX() x+; private: int x ; ; class B : public A public: B() y=1; int out() return y ;

28、 void addY() y+; private: int y ; ; void main() A a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,调用派生类公有成员函数 b.y +,7.2.1 访问控制,例7-3 私有数据成员的测试,#include class A public: A() x=1; int out() return x ; void addX() x+; private: int x ; ; class

29、 B : public A public: B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,调用基类版本同名函数 返回 b.x 的值,7.2.1 访问控制,例7-3 私有数据成员的测试,#include class A public: A() x=1; int out

30、() return x ; void addX() x+; private: int x ; ; class B : public A public: B() y=1; int out() return y ; void addY() y+; private: int y ; ; void main() A a ;cout “a.x=“ a.out() endl ;B b ;b.addX() ; b.addY() ;cout “b.x=“ b.A:out() endl ;cout “b.y=“ b.out() endl ; ,调用派生类版本同名函数 返回 b.y 的值,7.2.1 访问控制,例

31、7-3 私有数据成员的测试,7.2.1 访问控制,3.保护继承,派生类定义了与基类同名的成员,在派生类中访问同名成员时屏蔽了基类的同名成员在派生类中使用基类的同名成员,显式地使用类名限定符: 类名 : 成员,7.2.2 重名成员,7.2.2 重名成员,例: class base public :int a , b ; ; class derived : public base public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b = 2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,1

32、.重名数据成员,base a b,derived a b b c,例: class base public :int a , b ; ; class derived : public base public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b = 2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,1.重名数据成员,base a b,derived a b b c,例: class base public :int a , b ; ; class derived : public b

33、ase public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b = 2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,base a b,derived a b b c,访问从base 类继承 的数据成员a,1.重名数据成员,例: class base public :int a , b ; ; class derived : public base public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b =

34、2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,访问从base 类继承的 b,base a b,derived a b b c,1.重名数据成员,例: class base public :int a , b ; ; class derived : public base public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b = 2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,访问derived 类定义 的数据成员b,base a b,derived a

35、 b b c,1.重名数据成员,例: class base public :int a , b ; ; class derived : public base public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b = 2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,访问derived 类 定义的数据成员c,base a b,derived a b b c,1.重名数据成员,例: class base public :int a , b ; ; class derived : publ

36、ic base public : int b , c ; ; void f () derived d ;d . a = 1 ;d . base : b = 2 ;d . b = 3 ;d . c = 4 ; ;,7.2.2 重名成员,base a b,derived a b b c,基类成员的作用域延伸到所有派生类 派生类的重名成员屏蔽基类的同名成员,1.重名数据成员,/例7-4 #include class A public: int a1, a2 ;A( int i1=0, int i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t

37、“a2=“ a2 endl ; ; class B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员函数 ; void main() B b ; b.A:print(); b.printAB(); ,7.2.2 重名成员,2.重名成员函数,void print

38、() cout “a1=“ a1 t “a2=“ a2 endl ; ,void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; ,/例7-4 #include class A public: int a1, a2 ;A( int i1=0, int i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t “a2=“ a2 endl ; ; class B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j

39、2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员函数 ; void main() B b ; b.A:print(); b.printAB(); ,7.2.2 重名成员,2.重名成员函数,void print() cout “a1=“ a1 t “a2=“ a2 endl ; ,void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; ,派生类屏蔽基

40、类同名成员函数调用自身的成员函数,/例7-4 #include class A public: int a1, a2 ;A( int i1=0, int i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t “a2=“ a2 endl ; ; class B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:

41、print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员函数 ; void main() B b ; b.A:print(); b.printAB(); ,7.2.2 重名成员,2.重名成员函数,void print() cout “a1=“ a1 t “a2=“ a2 endl ; ,void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; ,派生类对象调用从基类继承的同名成员函数,/例7-4 #include class A public: int a1, a2 ;A( int i1=0, int

42、 i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t “a2=“ a2 endl ; ; class B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员函数 ; void main() B b ; b.

43、A:print(); b.printAB(); ,7.2.2 重名成员,2.重名成员函数,void print() cout “a1=“ a1 t “a2=“ a2 endl ; ,void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; ,基类this指针 指向派生类对象,/例7-4 #include class A public: int a1, a2 ;A( int i1=0, int i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t “a2=“ a2 endl ; ; class

44、B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员函数 ; void main() B b ; b.A:print(); b.printAB(); ,7.2.2 重名成员,2.重名成员函数,/例7-4 #include class A public: int

45、 a1, a2 ;A( int i1=0, int i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t “a2=“ a2 endl ; ; class B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员

46、函数 ; void main() B b ; b.A:print(); b.printAB(); ,7.2.2 重名成员,2.重名成员函数,/例7-4 #include class A public: int a1, a2 ;A( int i1=0, int i2=0 ) a1 = i1; a2 = i2; void print() cout “a1=“ a1 t “a2=“ a2 endl ; ; class B : public A public: int b1, b2 ;B( int j1=1, int j2=1 ) b1 = j1; b2 = j2; void print() /定义同名函数 cout “b1=“ b1 t “b2=“ b2 endl ; void printAB() A:print() ; /派生类对象调用基类版本同名成员函数print() ; /派生类对象调用自身的成员函数 ; void main() B b ; b.A:print(); b.printAB(); ,

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

当前位置:首页 > 教学课件 > 大学教育

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1