Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97916
  • 博文数量: 27
  • 博客积分: 550
  • 博客等级: 下士
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-20 10:47
文章分类
文章存档

2014年(1)

2011年(26)

分类: C/C++

2011-08-30 12:12:49

 
虚函数:父类中有虚函数,如果子类实例以父类的身份出现,那么调用虚函数的时候就会是子类的实现,而不是父类的实现,如果是普通方法,那么将会调用父类的实现



1.首先:强调一个概念 
       定义一个函数为虚函数,不代表函数为不被实现的函数。定义他为虚函数是为了允许用基类的指针来调用子类的这个函数。
       定义一个函数为纯虚函数,才代表函数没有被实现。定义他是为了实现一个接口,起到一个规范的作用,规范继承这个。类的程序员必须实现这个函数。 

2.关于实例化一个类: 
有纯虚函数的类是不可能生成类对象的,如果没有纯虚函数则可以。比如: 
Cpp代码 复制代码 收藏代码
  1. class CA    
  2. {    
  3. public:    
  4.     virtual void fun() = 0;  // 说明fun函数为纯虚函数    
  5.     virtual void fun1();    
  6. };    
  7.   
  8. class CB    
  9. {    
  10. public:    
  11.    virtual void fun();    
  12.    virtual void fun1();    
  13. };    
  14.   
  15. // CA,CB类的实现    
  16. ...    
  17.   
  18. void main()    
  19. {    
  20.     CA a;   // 不允许,因为类CA中有纯虚函数    
  21.     CB b;   // 可以,因为类CB中没有纯虚函数    
  22.     ...    
  23. }   
class CA { public: virtual void fun() = 0; // 说明fun函数为纯虚函数 virtual void fun1(); }; class CB { public: virtual void fun(); virtual void fun1(); }; // CA,CB类的实现 ... void main() { CA a; // 不允许,因为类CA中有纯虚函数 CB b; // 可以,因为类CB中没有纯虚函数 ... }  
3.虚函数在多态中间的使用: 
   多态一般就是通过指向基类的指针来实现的。 

4.有一点你必须明白,就是用父类的指针在运行时刻来调用子类: 
例如,有个函数是这样的: 
Cpp代码 复制代码 收藏代码
  1. void animal::fun1(animal *maybedog_maybehorse)    
  2. {    
  3.      maybedog_maybehorse->born();   
  4. }   
void animal::fun1(animal *maybedog_maybehorse) { maybedog_maybehorse->born(); }  

参数maybedog_maybehorse在编译时刻并不知道传进来的是dog类还是horse类,所以就把它设定为animal类,具体到运行时决定了才决定用那个函数。也就是说用父类指针通过虚函数来决定运行时刻到底是谁而指向谁的函数。

5.用虚函数 
Cpp代码 复制代码 收藏代码
  1. #include     
  2.   
  3. class animal    
  4. {    
  5. public:    
  6.      animal();    
  7.      ~animal();    
  8.      void fun1(animal *maybedog_maybehorse);    
  9.      virtual void born();    
  10. };    
  11.   
  12. void animal::fun1(animal *maybedog_maybehorse)    
  13. {    
  14.      maybedog_maybehorse->born();    
  15. }   
  16.   
  17. animal::animal() { }    
  18. animal::~animal() { }    
  19. void animal::born()    
  20. {    
  21.      cout<< "animal";    
  22. }    
  23. ///////////////////////horse   
  24. class horse:public animal    
  25. {    
  26. public:    
  27.      horse();    
  28.      ~horse();    
  29.      virtual void born();    
  30. };    
  31.   
  32. horse::horse() { }    
  33. horse::~horse() { }    
  34. void horse::born()   
  35. {    
  36.      cout<<"horse";    
  37. }    
  38. ///////////////////////main   
  39. void main()    
  40. {    
  41.      animal a;    
  42.      horse b;    
  43.      a.fun1(&b);    
  44. }    
  45.   
  46. //output: horse  
#include class animal { public: animal(); ~animal(); void fun1(animal *maybedog_maybehorse); virtual void born(); }; void animal::fun1(animal *maybedog_maybehorse) { maybedog_maybehorse->born(); } animal::animal() { } animal::~animal() { } void animal::born() { cout<< "animal"; } ///////////////////////horse class horse:public animal { public: horse(); ~horse(); virtual void born(); }; horse::horse() { } horse::~horse() { } void horse::born() { cout<<"horse"; } ///////////////////////main void main() { animal a; horse b; a.fun1(&b); } //output: horse 

6.不用虚函数 
Cpp代码 复制代码 收藏代码
  1. #include     
  2. class animal    
  3. {    
  4. public:    
  5.      animal();    
  6.      ~animal();    
  7.      void fun1(animal *maybedog_maybehorse);    
  8.      void born();    
  9. };    
  10.   
  11. void animal::fun1(animal *maybedog_maybehorse)    
  12. {    
  13.      maybedog_maybehorse->born();    
  14. }    
  15.   
  16. animal::animal() { }   
  17. animal::~animal() { }    
  18. void animal::born()    
  19. {    
  20.      cout<< "animal";    
  21. }    
  22. ////////////////////////horse   
  23. class horse:public animal    
  24. {    
  25. public:    
  26.      horse();    
  27.      ~horse();    
  28.      void born();    
  29. };    
  30.   
  31. horse::horse() { }    
  32. horse::~horse() { }    
  33. void horse::born()   
  34. {    
  35.      cout<<"horse";    
  36. }    
  37. ////////////////////main   
  38. void main()    
  39. {    
  40.      animal a;    
  41.      horse b;    
  42.      a.fun1(&b);    
  43. }    
  44. //output: animal  
阅读(1392) | 评论(0) | 转发(0) |
0

上一篇:设置生成core文件

下一篇:enum类型的作用

给主人留下些什么吧!~~