C++中的多态性和纯虚函数

来源:互联网 发布:http请求xml数据 编辑:程序博客网 时间:2024/05/17 23:12

C++的多态性:

    当C++中在继承时会发生函数覆盖(覆盖的概念我的上一篇blog),此时如果将子类对象的地址赋给父类对象的指针,用该指针调用函数时,当子类中有这样的函数,则调用子类的函数,如果没有,则调用父类的函数,这种技术也被称为迟绑定技术。。如果父类的函数没有加virtual,则此时会调用父类的函数,这也被称为早期绑定。下面看例子:

Animal.h

[cpp] view plaincopyprint?
  1. #ifndef Animal_H_H  
  2. #define Animal_H_H  
  3. class Animal{  
  4. public:  
  5.     Animal(int height,int weight);  
  6.     void eat();  
  7.     virtual void breath();  
  8.     void sleep();  
  9. };  
  10. #endif  
Fish.h

[cpp] view plaincopyprint?
  1. #include "Animal.h"  
  2. #ifndef Fish_H_H  
  3. #define Fish_H_H  
  4. class Fish:public Animal{  
  5. public:  
  6.      Fish();  
  7.      void breath();  
  8. private:  
  9.     const int a;  
  10. };  
  11. #endif  

Animal.cpp

[cpp] view plaincopyprint?
  1. #include "Animal.h"  
  2. #include <iostream.h>  
  3. Animal::Animal(int width,int height){  
  4.     cout<<"Animal construct"<<endl;  
  5. }  
  6.   
  7. void Animal::eat(){  
  8.     cout<<"Animal eat"<<endl;  
  9. }  
  10. void Animal::sleep(){  
  11.     cout<<"Animal sleep"<<endl;  
  12. }  
  13. void Animal::breath(){  
  14.     cout<<"Animal breath"<<endl;  
  15.   
  16.   
  17. }  

Fish.cpp

[cpp] view plaincopyprint?
  1. #include "Fish.h"  
  2. #include <iostream.h>  
  3. Fish::Fish():Animal(400,300),a(1){  
  4.     cout<<"Fish construct"<<endl;  
  5. }  
  6. void Fish::breath(){  
  7.     cout<<"Fish bubble"<<endl;  
  8. }  

Main.cpp

[cpp] view plaincopyprint?
  1. #include "Animal.h"  
  2. #include "Fish.h"  
  3. int main(){  
  4.     Fish fh;  
  5.     Animal *al;  
  6.     al=&fh;  
  7.     al->breath();  
  8.     return 0;  
  9. }  

此时就是延迟绑定,运行结果会显示Fish bubble,,如果将animal中breath函数的virtual去掉,则会显示Animal breath这就是早期绑定技术。

另外这段代码还可以结合函数覆盖 隐藏使用 将会更复杂,,下面有段代码和运行结果,大家自己看看并思考:

[cpp] view plaincopyprint?
  1. #include <iostream.h>  
  2. class Base{  
  3. public:  
  4.     virtual void xfn(int i){  
  5.         cout<<"Base::xfn(int i)"<<endl;  
  6.     }  
  7.     void yfn(float f){  
  8.         cout<<"Base::yfn(float f)"<<endl;  
  9.     }  
  10.     void zfn(){  
  11.         cout<<"Base::zfn()"<<endl;  
  12.     }  
  13. };  
  14. class Derived : public Base{  
  15. public :  
  16.     void xfn(int i){  
  17.         cout<<"Derived::xfn(int i)"<<endl;  
  18.     }  
  19.     void yfn(int c){  
  20.         cout<<"Derived::yfn(int c)"<<endl;  
  21.     }  
  22.     void zfn(){  
  23.         cout<<"Derived::zfn()"<<endl;  
  24.     }  
  25. };  
  26. int main(){  
  27.     Derived d;  
  28.     Base *pB=&d;  
  29.     Derived *pD=&d;  
  30.     pB->xfn(5);  
  31.     pD->xfn(5);  
  32.     pB->yfn(3.14f);  
  33.     pD->yfn(3.14f);  
  34.     pB->zfn();  
  35.     pD->zfn();  
  36. }  

运行结果:



最后介绍下纯虚函数,纯虚函数没有函数体实现,如同 virtual void breath()=0;此时对应的Animal类就变成了抽象类(含有纯虚函数的类称之为抽象类),抽象类不能实例化一个对象,但可以有指针。具体的实现由其派生类来实现。纯虚函数多用在一些方法行为的设计上,在设计基类时,不太好确定或将来的行为多种多样,而此行为又是必须的,我们就可以在基类的设计中,以纯虚函数来声明此种行为而不具体实现它。


0 0
原创粉丝点击