资源预览内容
第1页 / 共38页
第2页 / 共38页
第3页 / 共38页
第4页 / 共38页
第5页 / 共38页
第6页 / 共38页
第7页 / 共38页
第8页 / 共38页
第9页 / 共38页
第10页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数面向对象程序设计面向对象程序设计讲义讲义 第第 12 12 章章第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数12.1 12.1 多态性在多态性在C+C+中的体现中的体现 所谓多态性就是当不同对象收到相同的消息时产生不同所谓多态性就是当不同对象收到相同的消息时产生不同的动作。的动作。12.1.1 12.1.1 编译时的多态性编译时的多态性1. 1. 在一个类中说明的重载在一个类中说明的重载2. 2. 基类成员函数在派生类中重载基类成员函数在派生类中重载12.1.2 12.1.2 运行时的多态性运行时的多态性先看下面的例子:先看下面的例子:第第 12 12 章章 多态性与虚函数多态性与虚函数第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数#include#include#include#includeclass pointclass point protected: protected: int x,y; int x,y; public: public: point(int x,int y) point(int x,int y) point:x=x; point:y=y; point:x=x; point:y=y; virtual void show() / virtual void show() / 定义虚函数定义虚函数 putpixel(x,y,getcolor(); putpixel(x,y,getcolor();第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class circles:public point class circles:public point int radius; int radius; public: public: circles(int x,int y,int radius):point(x,y) circles(int x,int y,int radius):point(x,y) circles:radius=radius; circles:radius=radius; void show() void show() circles(x,y,radius); circles(x,y,radius); ;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数main()main()main()main() point p1(10,10); point p1(10,10); point p1(10,10); point p1(10,10); circles c1(100,100,50); circles c1(100,100,50); circles c1(100,100,50); circles c1(100,100,50); setcolor(14); setcolor(14); setcolor(14); setcolor(14); point *ptr; / point *ptr; / point *ptr; / point *ptr; /定义指向基类的指针定义指向基类的指针定义指向基类的指针定义指向基类的指针 ptr=&p1; / ptr=&p1; / ptr=&p1; / ptr=&p1; /指针指向基类对象指针指向基类对象指针指向基类对象指针指向基类对象p1p1p1p1 ptr-show(); / ptr-show(); / ptr-show(); / ptr-show(); /调用调用调用调用p1p1p1p1对象的对象的对象的对象的show()show()show()show() ptr=&c1; / ptr=&c1; / ptr=&c1; / ptr=&c1; /指针指向对象指针指向对象指针指向对象指针指向对象c1c1c1c1 ptr-show(); / ptr-show(); / ptr-show(); / ptr-show(); /调用调用调用调用c1c1c1c1对象的对象的对象的对象的show()show()show()show() 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数12.2 虚 函 数12.2.1 对象指针1. 一般对象的指针 语法与指向一般变量的指针相同。2. 引入派生类后的对象指针 任何被说明为指向基类的指针都可以指向它的公有派生类。使用派生类对象指针时应注意的问题:(1可以用一个声明让指向基类对象的指针指向它的公有派生的对象。禁止指向私有派生的对象。(2不能将一个声明为指向派生类对象的指针指向其基类的一个对象。第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数(3 3声明为指向基类对象的指针,当其指向派生类对象时,只声明为指向基类对象的指针,当其指向派生类对象时,只声明为指向基类对象的指针,当其指向派生类对象时,只声明为指向基类对象的指针,当其指向派生类对象时,只能利用它来直接访问派生类中从基类继承来的成员,不能访问公能利用它来直接访问派生类中从基类继承来的成员,不能访问公能利用它来直接访问派生类中从基类继承来的成员,不能访问公能利用它来直接访问派生类中从基类继承来的成员,不能访问公有派生类中特定的成员。有派生类中特定的成员。有派生类中特定的成员。有派生类中特定的成员。12.2.2 12.2.2 为什么要引入虚函数为什么要引入虚函数为什么要引入虚函数为什么要引入虚函数#include#includeclass base class base public: public: void who() void who() cout“this is the class of base !n”; cout“this is the class of base !n”; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class derive1:public base class derive1:public base class derive1:public base class derive1:public base public: public: public: public: void who() void who() void who() void who() cout“this is the class of derive1 !n”; cout“this is the class of derive1 !n”; cout“this is the class of derive1 !n”; cout“this is the class of derive1 !n”; ; ; ; ;class derive2:public base class derive2:public base class derive2:public base class derive2:public base public: public: public: public: void who() void who() void who() void who() cout“this is the class of derive2 !n”; cout“this is the class of derive2 !n”; cout“this is the class of derive2 !n”; coutwho(); p-who(); p=&obj2; p=&obj2; p-who(); p-who(); p=&obj3; p=&obj3; p-who(); p-who(); obj2.who(); obj2.who(); obj3.who(); obj3.who(); 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数运行结果:运行结果:运行结果:运行结果:this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of base!this is the class of derive1 !this is the class of derive1 !this is the class of derive1 !this is the class of derive1 !this is the class of derive2 !this is the class of derive2 !this is the class of derive2 !this is the class of derive2 ! 从结果可以看出,通过指针引起的普通成员函数调用,从结果可以看出,通过指针引起的普通成员函数调用,从结果可以看出,通过指针引起的普通成员函数调用,从结果可以看出,通过指针引起的普通成员函数调用,仅仅与指针的类型有关,而与此刻正指向什么对象无关。仅仅与指针的类型有关,而与此刻正指向什么对象无关。仅仅与指针的类型有关,而与此刻正指向什么对象无关。仅仅与指针的类型有关,而与此刻正指向什么对象无关。第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数12.2.3 12.2.3 12.2.3 12.2.3 虚函数的定义及使用虚函数的定义及使用虚函数的定义及使用虚函数的定义及使用1. 1. 1. 1. 虚函数的定义虚函数的定义虚函数的定义虚函数的定义#include#include#include#includeclass base class base class base class base /. /. /. /. public: public: public: public: virtual void who() / virtual void who() / virtual void who() / virtual void who() /定义虚函数定义虚函数定义虚函数定义虚函数 cout cout cout cout“ base!n base!n base!n base!n”; ; ; ; ; ; ; ;class frist:public base class frist:public base class frist:public base class frist:public base /. /. /. /. public: public: public: public: void who() / void who() / void who() / void who() /重新定义虚函数重新定义虚函数重新定义虚函数重新定义虚函数 cout cout cout cout“ the first derivationn the first derivationn the first derivationn the first derivationn”; ; ; ; ; ; ; ;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class second:public base class second:public base class second:public base class second:public base /. /. /. /. public: public: public: public: void who() / void who() / void who() / void who() /重新定义虚函数重新定义虚函数重新定义虚函数重新定义虚函数 cout cout cout coutwho(); / ptr-who(); / ptr-who(); / ptr-who(); /调用调用调用调用basebasebasebase类的类的类的类的who( )who( )who( )who( )版本版本版本版本第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数 ptr=&obj2; ptr=&obj2; ptr=&obj2; ptr=&obj2; ptr-who(); / ptr-who(); / ptr-who(); / ptr-who(); /调用调用调用调用firstfirstfirstfirst类的类的类的类的who()who()who()who()版本版本版本版本 ptr=&obj3; ptr=&obj3; ptr=&obj3; ptr=&obj3; ptr-who(); / ptr-who(); / ptr-who(); / ptr-who(); /调用调用调用调用secondsecondsecondsecond类的类的类的类的who()who()who()who()版本版本版本版本 运行结果:运行结果:运行结果:运行结果: base base base base the first derivation the first derivation the first derivation the first derivation the second derivation the second derivation the second derivation the second derivation第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数2. 2. 2. 2. 虚函数与重载函数的关系虚函数与重载函数的关系虚函数与重载函数的关系虚函数与重载函数的关系3. 3. 3. 3. 多继承中的虚函数多继承中的虚函数多继承中的虚函数多继承中的虚函数#include#include#include#includeclass a class a class a class a public: public: public: public: virtual void f() / virtual void f() / virtual void f() / virtual void f() /定义定义定义定义f()f()f()f()为虚函数为虚函数为虚函数为虚函数 cout cout cout cout“class anclass anclass anclass an”; ; ; ; ; ; ; ;class b class b class b class b public: public: public: public: void f() / void f() / void f() / void f() /定义定义定义定义f()f()f()f()为一般函数为一般函数为一般函数为一般函数 cout cout cout cout“class bnclass bnclass bnclass bn”; ; ; ; ; ; ; ;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class aa: public a, public b class aa: public a, public b class aa: public a, public b class aa: public a, public b public: public: public: public: void f() void f() void f() void f() cout cout cout coutf(); / ptr1-f(); / ptr1-f(); / ptr1-f(); /调用调用调用调用a a a a类的类的类的类的f()f()f()f() ptr2=&obj2; / ptr2=&obj2; / ptr2=&obj2; / ptr2=&obj2; /将指针将指针将指针将指针ptr2ptr2ptr2ptr2指向指向指向指向b b b b类对象类对象类对象类对象 ptr2-f(); / ptr2-f(); / ptr2-f(); / ptr2-f(); /调用调用调用调用b b b b类的类的类的类的f()f()f()f()第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数 ptr1=&obj3; / ptr1=&obj3; /将指针将指针将指针将指针ptr1ptr1指向指向指向指向a a类的派生类类的派生类类的派生类类的派生类aaaa类的对象类的对象类的对象类的对象 ptr1-f(); / ptr1-f(); /调用调用调用调用aaaa类的类的类的类的f(),f(),此时的此时的此时的此时的f()f()为虚函数为虚函数为虚函数为虚函数 ptr2=&obj3; / ptr2=&obj3; /将指针将指针将指针将指针ptr2ptr2指向指向指向指向b b类的派生类类的派生类类的派生类类的派生类aaaa类的对象类的对象类的对象类的对象 ptr2-f(); / ptr2-f(); /调用调用调用调用b b类的类的类的类的f(),f(),此处此处此处此处f()f()为非虚函数,而为非虚函数,而为非虚函数,而为非虚函数,而ptr2ptr2 / /又为又为又为又为b b的指针的指针的指针的指针 运行结果:运行结果:运行结果:运行结果:class aclass aclass bclass bclass aaclass aaclass bclass b第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数若一个派生类,它的多个基类中有公共的基类,在公共基类若一个派生类,它的多个基类中有公共的基类,在公共基类若一个派生类,它的多个基类中有公共的基类,在公共基类若一个派生类,它的多个基类中有公共的基类,在公共基类中定义一个虚函数,则多级派生以后仍可以重新定义虚函数。中定义一个虚函数,则多级派生以后仍可以重新定义虚函数。中定义一个虚函数,则多级派生以后仍可以重新定义虚函数。中定义一个虚函数,则多级派生以后仍可以重新定义虚函数。使用级联式派生时要注意,指向派生类的指针不能继承。也使用级联式派生时要注意,指向派生类的指针不能继承。也使用级联式派生时要注意,指向派生类的指针不能继承。也使用级联式派生时要注意,指向派生类的指针不能继承。也就是说,基类的指针可以指向它的派生类,但不能再指向它就是说,基类的指针可以指向它的派生类,但不能再指向它就是说,基类的指针可以指向它的派生类,但不能再指向它就是说,基类的指针可以指向它的派生类,但不能再指向它的派生类的派生类。的派生类的派生类。的派生类的派生类。的派生类的派生类。4. 4. 4. 4. 基类构造函数调用虚函数基类构造函数调用虚函数基类构造函数调用虚函数基类构造函数调用虚函数 自学自学自学自学第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数12.2.4 12.2.4 12.2.4 12.2.4 虚函数举例虚函数举例虚函数举例虚函数举例例例例例 3 3 3 3 /-/-/-/-#include #include #include #include #pragma hdrstop#pragma hdrstop#pragma hdrstop#pragma hdrstop#include U12_2_4_3.h#include U12_2_4_3.h#include U12_2_4_3.h#include U12_2_4_3.h/-/-/-/-#pragma package(smart_init)#pragma package(smart_init)#pragma package(smart_init)#pragma package(smart_init)#pragma resource *.dfm#pragma resource *.dfm#pragma resource *.dfm#pragma resource *.dfmTf12_2_4_3 *f12_2_4_3;Tf12_2_4_3 *f12_2_4_3;Tf12_2_4_3 *f12_2_4_3;Tf12_2_4_3 *f12_2_4_3;/*-/*-/*-/*-/enum bool false,true;/enum bool false,true;/enum bool false,true;/enum bool false,true;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数struct element /struct element /struct element /struct element /定义链表中的结点结构定义链表中的结点结构定义链表中的结点结构定义链表中的结点结构 int val; int val; int val; int val; element *next; element *next; element *next; element *next; ; ; ;class list /class list /class list /class list /定义链表类定义链表类定义链表类定义链表类 element *elems; element *elems; element *elems; element *elems; public: public: public: public: list()elems=0; list()elems=0; list()elems=0; list()elems=0; list(); list(); list(); list(); virtual bool insert(int); / virtual bool insert(int); / virtual bool insert(int); / virtual bool insert(int); /定义虚函数定义虚函数定义虚函数定义虚函数 virtual bool deletes(int); / virtual bool deletes(int); / virtual bool deletes(int); / virtual bool deletes(int); /定义虚函数定义虚函数定义虚函数定义虚函数 bool contain(int); bool contain(int); bool contain(int); bool contain(int); void print(); void print(); void print(); void print();第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class set:public list class set:public list class set:public list class set:public list int card; int card; int card; int card; public: public: public: public: set()card=0; set()card=0; set()card=0; set()card=0; bool insert(int); / bool insert(int); / bool insert(int); / bool insert(int); /重定义虚函数重定义虚函数重定义虚函数重定义虚函数 bool deletes(int); / bool deletes(int); / bool deletes(int); / bool deletes(int); /重定义虚函数重定义虚函数重定义虚函数重定义虚函数; ; ; ;list:list()list:list()list:list()list:list() element *tmp=elems; element *tmp=elems; element *tmp=elems; element *tmp=elems; for(element *elem=elems;elem!=0;) for(element *elem=elems;elem!=0;) for(element *elem=elems;elem!=0;) for(element *elem=elems;elem!=0;) tmp=elem; tmp=elem; tmp=elem; tmp=elem; elem=elem-next; elem=elem-next; elem=elem-next; elem=elem-next; delete tmp; delete tmp; delete tmp; delete tmp; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数bool list:insert(int val) /bool list:insert(int val) /bool list:insert(int val) /bool list:insert(int val) /定义定义定义定义listlistlistlist类中插入元素的成员函数类中插入元素的成员函数类中插入元素的成员函数类中插入元素的成员函数 element *elem=new element; / element *elem=new element; / element *elem=new element; / element *elem=new element; /为新元素分配内存为新元素分配内存为新元素分配内存为新元素分配内存 if(elem!=0) if(elem!=0) if(elem!=0) if(elem!=0) elem-val=val; / elem-val=val; / elem-val=val; / elem-val=val; /将元素插入链表头将元素插入链表头将元素插入链表头将元素插入链表头 elem-next=elems; elem-next=elems; elem-next=elems; elem-next=elems; elems=elem; elems=elem; elems=elem; elems=elem; return true; return true; return true; return true; else return false; else return false; else return false; else return false; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数bool list:deletes(int val) /bool list:deletes(int val) /bool list:deletes(int val) /bool list:deletes(int val) /定义定义定义定义listlistlistlist类中删除元素的成员函数类中删除元素的成员函数类中删除元素的成员函数类中删除元素的成员函数 if(elems=0)return false; / if(elems=0)return false; / if(elems=0)return false; / if(elems=0)return false; /若表为空,返回若表为空,返回若表为空,返回若表为空,返回falsefalsefalsefalse element *tmp=elems; element *tmp=elems; element *tmp=elems; element *tmp=elems; if(elems-val=val) if(elems-val=val) if(elems-val=val) if(elems-val=val) / / / /若待删除的元素为链表头元素若待删除的元素为链表头元素若待删除的元素为链表头元素若待删除的元素为链表头元素 elems=elems-next; elems=elems-next; elems=elems-next; elems=elems-next; delete tmp; delete tmp; delete tmp; delete tmp; return true; return true; return true; return true; else else else else第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数for(element *elem=elems;elem-next!=0;elem-next)for(element *elem=elems;elem-next!=0;elem-next)for(element *elem=elems;elem-next!=0;elem-next)for(element *elem=elems;elem-next!=0;elem-next) if(elem-next-val=val) if(elem-next-val=val) if(elem-next-val=val) if(elem-next-val=val) / / / /循环查找待删除元素循环查找待删除元素循环查找待删除元素循环查找待删除元素 tmp=elem-next; tmp=elem-next; tmp=elem-next; tmp=elem-next; elem-next=tmp-next; elem-next=tmp-next; elem-next=tmp-next; elem-next=tmp-next; delete tmp; delete tmp; delete tmp; delete tmp; return true; return true; return true; return true; return false; return false; return false; return false; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数bool list:contain(int val)bool list:contain(int val)bool list:contain(int val)bool list:contain(int val) / / / /判元素判元素判元素判元素valvalvalval在链表中是否存在在链表中是否存在在链表中是否存在在链表中是否存在 if(elems=0)return false; if(elems=0)return false; if(elems=0)return false; if(elems=0)return false; if(elems-val=val)return true; if(elems-val=val)return true; if(elems-val=val)return true; if(elems-val=val)return true; else else else else for(element *elem=elems;elem-next!=0;elem=elem- for(element *elem=elems;elem-next!=0;elem=elem- for(element *elem=elems;elem-next!=0;elem=elem- for(element *elem=elems;elem-next!=0;elem=elem-next)next)next)next) if(elem-next-val=val) if(elem-next-val=val) if(elem-next-val=val) if(elem-next-val=val) return true; return true; return true; return true; return false; return false; return false; return false;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数void list:print()void list:print()void list:print()void list:print() if(elems=0)return; if(elems=0)return; if(elems=0)return; if(elems=0)return; int i=1; int i=1; int i=1; int i=1; static int j=0; static int j=0; static int j=0; static int j=0; j+; j+; j+; j+; for(element *elem=elems;elem!=0;elem=elem-next) for(element *elem=elems;elem!=0;elem=elem-next) for(element *elem=elems;elem!=0;elem=elem-next) for(element *elem=elems;elem!=0;elem=elem-next) f12_2_4_3-Canvas-TextOut(30*i+,20*j, f12_2_4_3-Canvas-TextOut(30*i+,20*j, f12_2_4_3-Canvas-TextOut(30*i+,20*j, f12_2_4_3-Canvas-TextOut(30*i+,20*j, IntToStr(elem-val); IntToStr(elem-val); IntToStr(elem-val); IntToStr(elem-val);第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数bool set:insert(int val)bool set:insert(int val)bool set:insert(int val)bool set:insert(int val) / / / /在在在在setsetsetset类中的类中的类中的类中的insertinsertinsertinsert的重定义版本的重定义版本的重定义版本的重定义版本 if(!contain(val)&list:insert(val) if(!contain(val)&list:insert(val) if(!contain(val)&list:insert(val) if(!contain(val)&list:insert(val) / / / /先判断此元素是否已存在,然后再调用基类的此函数版本先判断此元素是否已存在,然后再调用基类的此函数版本先判断此元素是否已存在,然后再调用基类的此函数版本先判断此元素是否已存在,然后再调用基类的此函数版本 +card; +card; +card; +card; return true; return true; return true; return true; return false; return false; return false; return false; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数bool set:deletes(int val)bool set:deletes(int val)bool set:deletes(int val)bool set:deletes(int val) / / / /在在在在setsetsetset类中的类中的类中的类中的deletesdeletesdeletesdeletes重定义版本重定义版本重定义版本重定义版本 if(list:deletes(val) / if(list:deletes(val) / if(list:deletes(val) / if(list:deletes(val) /调用基类中的此函数版本调用基类中的此函数版本调用基类中的此函数版本调用基类中的此函数版本 -card; -card; -card; -card; return true; return true; return true; return true; return false; return false; return false; return false; /*-/*-/*-/*-_fastcall Tf12_2_4_3:Tf12_2_4_3(TComponent* Owner)_fastcall Tf12_2_4_3:Tf12_2_4_3(TComponent* Owner)_fastcall Tf12_2_4_3:Tf12_2_4_3(TComponent* Owner)_fastcall Tf12_2_4_3:Tf12_2_4_3(TComponent* Owner) : TForm(Owner) : TForm(Owner) : TForm(Owner) : TForm(Owner) 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数void _fastcall Tf12_2_4_3:btnRunClick(TObject *Sender)void _fastcall Tf12_2_4_3:btnRunClick(TObject *Sender)void _fastcall Tf12_2_4_3:btnRunClick(TObject *Sender)void _fastcall Tf12_2_4_3:btnRunClick(TObject *Sender) list *ptr,list1; / list *ptr,list1; / list *ptr,list1; / list *ptr,list1; /定义基类对象定义基类对象定义基类对象定义基类对象list1list1list1list1和基类指针和基类指针和基类指针和基类指针ptrptrptrptr set set1; / set set1; / set set1; / set set1; /定义定义定义定义setsetsetset类对象类对象类对象类对象 ptr=&list1; / ptr=&list1; / ptr=&list1; / ptr=&list1; /将指针将指针将指针将指针ptrptrptrptr指向指向指向指向list1list1list1list1对象对象对象对象 ptr-insert(30); / ptr-insert(30); / ptr-insert(30); / ptr-insert(30); /调用调用调用调用listlistlistlist类中的类中的类中的类中的insertinsertinsertinsert版本版本版本版本 ptr-insert(40); ptr-insert(40); ptr-insert(40); ptr-insert(40); ptr-insert(543); ptr-insert(543); ptr-insert(543); ptr-insert(543); ptr-insert(40); ptr-insert(40); ptr-insert(40); ptr-insert(40); ptr-print(); / ptr-print(); / ptr-print(); / ptr-print(); /调用调用调用调用listlistlistlist类中的成员函数类中的成员函数类中的成员函数类中的成员函数 ptr=&set1; / ptr=&set1; / ptr=&set1; / ptr=&set1; /将指针将指针将指针将指针ptrptrptrptr指向指向指向指向setsetsetset对象对象对象对象 ptr-insert(23); / ptr-insert(23); / ptr-insert(23); / ptr-insert(23); /调用调用调用调用setsetsetset类中的类中的类中的类中的insertinsertinsertinsert版本版本版本版本 ptr-insert(672); ptr-insert(672); ptr-insert(672); ptr-insert(672);第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数 ptr-insert(456); ptr-insert(456); ptr-insert(456); ptr-insert(456); ptr-insert(23); ptr-insert(23); ptr-insert(23); ptr-insert(23); ptr-print(); / ptr-print(); / ptr-print(); / ptr-print(); /调用调用调用调用listlistlistlist类中的成员函数类中的成员函数类中的成员函数类中的成员函数 /-/-/-/-void _fastcall Tf12_2_4_3:btnEndClick(TObject *Sender)void _fastcall Tf12_2_4_3:btnEndClick(TObject *Sender)void _fastcall Tf12_2_4_3:btnEndClick(TObject *Sender)void _fastcall Tf12_2_4_3:btnEndClick(TObject *Sender) Close(); Close(); Close(); Close(); /-/-/-/-第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数12.3 12.3 抽抽 象象 类类12.3.1 12.3.1 纯虚函数与抽象类纯虚函数与抽象类1. 1. 纯虚函数的概念纯虚函数的概念定义形式:定义形式:virtual type func_name(virtual type func_name(参数表参数表)=0;)=0;本卷须知:在基类中定义为纯虚函数的函数,在本卷须知:在基类中定义为纯虚函数的函数,在任何派任何派生类中都必须定义自己的版本,否则将引起语法生类中都必须定义自己的版本,否则将引起语法错误。错误。第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数2. 2. 2. 2. 抽象类的概念抽象类的概念抽象类的概念抽象类的概念如果类中至少有一个纯虚函数,那么就称该类为抽象类。如果类中至少有一个纯虚函数,那么就称该类为抽象类。如果类中至少有一个纯虚函数,那么就称该类为抽象类。如果类中至少有一个纯虚函数,那么就称该类为抽象类。使用抽象类的几点规定:使用抽象类的几点规定:使用抽象类的几点规定:使用抽象类的几点规定:(1 1 1 1抽象类只能用作其它类的基类,不能建立抽象类对象。抽象类只能用作其它类的基类,不能建立抽象类对象。抽象类只能用作其它类的基类,不能建立抽象类对象。抽象类只能用作其它类的基类,不能建立抽象类对象。(2 2 2 2抽象类不能用作参数类型、函数返回类型或显式转换的抽象类不能用作参数类型、函数返回类型或显式转换的抽象类不能用作参数类型、函数返回类型或显式转换的抽象类不能用作参数类型、函数返回类型或显式转换的类型。类型。类型。类型。(3 3 3 3可以声明指向抽象类的指针和引用,此指针可以指可以声明指向抽象类的指针和引用,此指针可以指可以声明指向抽象类的指针和引用,此指针可以指可以声明指向抽象类的指针和引用,此指针可以指向它的派生类,进而实现多态性。向它的派生类,进而实现多态性。向它的派生类,进而实现多态性。向它的派生类,进而实现多态性。第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数举例说明如下:举例说明如下:举例说明如下:举例说明如下:class figure class figure class figure class figure / / / / public: public: public: public: virtual draw()=0; virtual draw()=0; virtual draw()=0; virtual draw()=0; / / / /; ; ; ;figure a; /figure a; /figure a; /figure a; /错误,不能建立抽象类对象错误,不能建立抽象类对象错误,不能建立抽象类对象错误,不能建立抽象类对象figure *ptr; /figure *ptr; /figure *ptr; /figure *ptr; /正确,可以声明指向抽象类的指针正确,可以声明指向抽象类的指针正确,可以声明指向抽象类的指针正确,可以声明指向抽象类的指针figure f(); /figure f(); /figure f(); /figure f(); /错误,抽象类不能作为函数的返回类型错误,抽象类不能作为函数的返回类型错误,抽象类不能作为函数的返回类型错误,抽象类不能作为函数的返回类型void g(figure); /void g(figure); /void g(figure); /void g(figure); /错误,抽象类不能作为函数的参数类型错误,抽象类不能作为函数的参数类型错误,抽象类不能作为函数的参数类型错误,抽象类不能作为函数的参数类型figure &h(figure &); /figure &h(figure &); /figure &h(figure &); /figure &h(figure &); /正确,可以声明抽象类的引用正确,可以声明抽象类的引用正确,可以声明抽象类的引用正确,可以声明抽象类的引用第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数12.3.2 12.3.2 12.3.2 12.3.2 纯虚函数多态性的体现纯虚函数多态性的体现纯虚函数多态性的体现纯虚函数多态性的体现例例例例 2 2 2 2#include#include#include#includeclass container /class container /class container /class container /定义一个抽象类定义一个抽象类定义一个抽象类定义一个抽象类 protected: protected: protected: protected: double radius; double radius; double radius; double radius; public: public: public: public: container(double radius) container(double radius) container(double radius) container(double radius) container:radius= radius; container:radius= radius; container:radius= radius; container:radius= radius; virtual double surface_area()=0; / virtual double surface_area()=0; / virtual double surface_area()=0; / virtual double surface_area()=0; /纯虚函数纯虚函数纯虚函数纯虚函数 virtual double volume()=0; / virtual double volume()=0; / virtual double volume()=0; / virtual double volume()=0; /纯虚函数纯虚函数纯虚函数纯虚函数; ; ; ;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class cube:public container /class cube:public container /class cube:public container /class cube:public container /定义正方体类定义正方体类定义正方体类定义正方体类 public: public: public: public: cube(double radius):container(radius) ; cube(double radius):container(radius) ; cube(double radius):container(radius) ; cube(double radius):container(radius) ; double surface_area(); / double surface_area(); / double surface_area(); / double surface_area(); /虚函数在本类中的定义版本虚函数在本类中的定义版本虚函数在本类中的定义版本虚函数在本类中的定义版本 double volume(); double volume(); double volume(); double volume(); ; ; ; ;class sphere:public container /class sphere:public container /class sphere:public container /class sphere:public container /定义球体类定义球体类定义球体类定义球体类 public: public: public: public: sphere(double radius):container(radius) ; sphere(double radius):container(radius) ; sphere(double radius):container(radius) ; sphere(double radius):container(radius) ; double surface_area(); / double surface_area(); / double surface_area(); / double surface_area(); /虚函数在本类中的定义版本虚函数在本类中的定义版本虚函数在本类中的定义版本虚函数在本类中的定义版本 double volume(); double volume(); double volume(); double volume(); ; ; ; ;第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数class cylinder:public container /class cylinder:public container /class cylinder:public container /class cylinder:public container /定义圆柱体类定义圆柱体类定义圆柱体类定义圆柱体类 double height; double height; double height; double height; public: public: public: public: cylinder(double radius,double cylinder(double radius,double cylinder(double radius,double cylinder(double radius,double height):container(radius) height):container(radius) height):container(radius) height):container(radius) cylinder:height=height; cylinder:height=height; cylinder:height=height; cylinder:height=height; double surface_area(); / double surface_area(); / double surface_area(); / double surface_area(); /虚函数在本类中的定义版本虚函数在本类中的定义版本虚函数在本类中的定义版本虚函数在本类中的定义版本 double volume(); double volume(); double volume(); double volume(); ; ; ; ;double cube:double surface_area()double cube:double surface_area()double cube:double surface_area()double cube:double surface_area() return(radius*radius*6); return(radius*radius*6); return(radius*radius*6); return(radius*radius*6); 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数double cube:volume()double cube:volume()double cube:volume()double cube:volume() return(radius*radius*radius); return(radius*radius*radius); return(radius*radius*radius); return(radius*radius*radius); double sphere:double surface_area()double sphere:double surface_area()double sphere:double surface_area()double sphere:double surface_area() return 4*3.1416*radius*radius; return 4*3.1416*radius*radius; return 4*3.1416*radius*radius; return 4*3.1416*radius*radius; double sphere:volume()double sphere:volume()double sphere:volume()double sphere:volume() return 3.1416*radius*radius*radius*4/3; return 3.1416*radius*radius*radius*4/3; return 3.1416*radius*radius*radius*4/3; return 3.1416*radius*radius*radius*4/3; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数double cylinder :double surface_area()double cylinder :double surface_area()double cylinder :double surface_area()double cylinder :double surface_area() return 3.1416*2*radius*(height+radius); return 3.1416*2*radius*(height+radius); return 3.1416*2*radius*(height+radius); return 3.1416*2*radius*(height+radius); double cylinder:volume()double cylinder:volume()double cylinder:volume()double cylinder:volume() return 3.1416*radius*radius*height; return 3.1416*radius*radius*height; return 3.1416*radius*radius*height; return 3.1416*radius*radius*height; 第第第第 12 12 12 12 章章章章 多态性与虚函数多态性与虚函数多态性与虚函数多态性与虚函数main()main()main()main() container *ptr; / container *ptr; / container *ptr; / container *ptr; / cube obj1(5); cube obj1(5); cube obj1(5); cube obj1(5); sphere obj2(5); sphere obj2(5); sphere obj2(5); sphere obj2(5); cylinder obj3(5,5); cylinder obj3(5,5); cylinder obj3(5,5); cylinder obj3(5,5); ptr=&obj1; ptr=&obj1; ptr=&obj1; ptr=&obj1; ptr=&obj2; ptr=&obj2; ptr=&obj2; ptr=&obj2; ptr=&obj3; ptr=&obj3; ptr=&obj3; ptr=&obj3;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号