c语言程序设计(郑莉第四版)课件9

上传人:tian****1990 文档编号:74095212 上传时间:2019-01-26 格式:PPT 页数:114 大小:1.84MB
返回 下载 相关 举报
c语言程序设计(郑莉第四版)课件9_第1页
第1页 / 共114页
c语言程序设计(郑莉第四版)课件9_第2页
第2页 / 共114页
c语言程序设计(郑莉第四版)课件9_第3页
第3页 / 共114页
c语言程序设计(郑莉第四版)课件9_第4页
第4页 / 共114页
c语言程序设计(郑莉第四版)课件9_第5页
第5页 / 共114页
点击查看更多>>
资源描述

《c语言程序设计(郑莉第四版)课件9》由会员分享,可在线阅读,更多相关《c语言程序设计(郑莉第四版)课件9(114页珍藏版)》请在金锄头文库上搜索。

1、多态性(Polymorphism)是指一个名字,多种语义;或界面 相同,多种实现。 重载函数是多态性的一种简单形式。 虚函数允许函数调用与函数体的联系在运行时才进行,称为 动态联编。,第9章 虚函数与多态性,9.1 静态联编,9.2 类指针的关系,9.3 虚函数与动态联编,9.4 纯虚函数与抽象类,9.5 虚函数和多态性的应用,小结,第9章 虚函数与多态性,联编是指一个程序模块、代码之间互相关联的过程。 静态联编,是程序的匹配、连接在编译阶段实现,也称为早期匹配。 重载函数使用静态联编。 动态联编是指程序联编推迟到运行时进行,所以又称为晚期联编。 switch 语句和 if 语句是动态联编的例

2、子。,9.1 静态联编,9.1 静态联编,普通成员函数重载可表达为两种形式:,1. 在一个类说明中重载,例如: void Show ( int , char ) ; void Show ( char * , float ) ;,9.1 静态联编,普通成员函数重载可表达为两种形式:,1. 在一个类说明中重载,例如: void Show ( int , char ); 与 void Show ( char * , float ); 不是同一函数,编译能够区分,2. 基类的成员函数在派生类重载。有 3 种编译区分方法:,(1)根据参数的特征加以区分,9.1 静态联编,普通成员函数重载可表达为两种形式

3、:,1. 在一个类说明中重载,2. 基类的成员函数在派生类重载。有 3 种编译区分方法:,(1)根据参数的特征加以区分,例如: A : Show ( ); 有别于 B : Show ( );,(2)使用“ : ”加以区分,9.1 静态联编,普通成员函数重载可表达为两种形式:,1. 在一个类说明中重载,2. 基类的成员函数在派生类重载。有 3 种编译区分方法:,(1)根据参数的特征加以区分,(2)使用“ : ”加以区分,例如: Aobj . Show ( ) 调用 A : Show ( ) Bobj . Show ( ) 调用 B : Show ( ),(3)根据类对象加以区分,根据this指针

4、类型区分,基类指针和派生类指针与基类对象和派生类对象4种可能匹配: 直接用基类指针引用基类对象; 直接用派生类指针引用派生类对象; 用基类指针引用一个派生类对象; 用派生类指针引用一个基类对象。,9.2 类指针的关系,9.2 类指针的关系,例如: A * p ; / 指向类型 A 的对象的指针 A A_obj ; / 类型 A 的对象 B B_obj ; / 类型 B 的对象 p = / p 指向类型 B 的对象,它是 A 的派生类,利用 p,可以通过 B_obj 访问所有从 A 类继承的元素 , 但不能用 p访问 B 类自定义的元素 (除非用了显式类型转换),9.2.1 基类指针引用派生类对

5、象,9.2.1 基类指针引用派生类对象,#include #include using namespace std ; class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,例9-1 使用基类指针引用派生类对象,9.2.1 基类指针引用派生类对象,#include #include using namespace std ;

6、 class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,基类指针,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,#include #include using namespace std ; class A_class char name20 ; public : void put_name( c

7、har * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,基类指针 指向基类对象,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,#include #include using namespace std ; class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name(

8、) cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,基类指针 调用基类成员函数,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,#include #include using namespace std ; class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - sh

9、ow_name() ; A_p = ,基类指针 指向派生类对象,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,#include #include using namespace std ; class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,调用 从基类继承的成员函数,9.2.1 基类指针引用派

10、生类对象,例9-1 使用基类指针引用派生类对象,#include #include using namespace std ; class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,用派生类对象 调用派生类的成员函数,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,#include #include

11、 using namespace std ; class A_class char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,对基类指针强类型转换 调用派生类的成员函数,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,#include #include using namespace std ; class A_class

12、char name20 ; public : void put_name( char * s ) strcpy_s( name, s ) ; void show_name() cout put_name( “Wang xiao hua“ ) ; A_p - show_name() ; A_p = ,9.2.1 基类指针引用派生类对象,例9-1 使用基类指针引用派生类对象,派生类指针只有经过强制类型转换之后,才能引用基类对象,9.2.2 派生类指针引用基类对象,9.2.2 派生类指针引用基类对象,#include using namespace std ; class Date public:

13、Date( int y, int m, int d ) SetDate( y, m, d ); void SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; void Print() cout Print(); cout hours : minutes : seconds n ; private: int hours , minutes , seconds ; ; int main() DateTime dt( 2009, 1, 1, 12, 30, 0 ) ; dt.Print() ; ,例9-2 日期时间程序。在派

14、生类中调用基类同名成员函数,9.2.2 派生类指针引用基类对象,#include #include class Date public: Date( int y, int m, int d ) SetDate( y, m, d ); void SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; void Print() cout Print(); cout hours : minutes : seconds n ; private: int hours , minutes , seconds ; ; int main(

15、) DateTime dt( 2009, 1, 1, 12, 30, 0 ) ; dt.Print() ; ,( ( Date * ) this ) - Print();,对 this 指针作类型转换 调用基类成员函数,9.2.2 派生类指针引用基类对象,例9-2 日期时间程序。在派生类中调用基类同名成员函数,#include using namespace std ; class Date public: Date( int y, int m, int d ) SetDate( y, m, d ); void SetDate( int y, int m, int d ) year = y ; month = m ; day = d ; void Print() cout Print(); cout hours : minutes : seconds n ; private: int hours , minutes , seconds ; ; int main() DateTime dt( 2009, 1, 1, 12, 30, 0 ) ; dt.Print() ; ,( ( Date * ) this ) - Print();,9.2.2 派生类指针引用基类对象,例9-2 日期时间程序

展开阅读全文
相关资源
相关搜索

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

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号