Virtual_Function

来源:互联网 发布:nice软件电脑版 编辑:程序博客网 时间:2024/05/16 12:40
Code:
  1. #include<iostream.h>   
  2. class Father   
  3. {   
  4. public:   
  5.     virtual void output()   
  6.     {cout<<"Father.output() called"<<endl;}   
  7. };   
  8.   
  9. class Sun:public Father   
  10. {   
  11. public:   
  12.     void output()   
  13.     {cout<<"Sun.output() called"<<endl;}   
  14. };   
  15.   
  16. int main()   
  17. {   
  18.   Father father,*pb;   
  19.   
  20.   Sun sun;   
  21.   
  22.   pb=&father;   
  23.   
  24.   pb->output();   
  25.   
  26.   pb=&sun;   
  27.   
  28.   pb->output();   
  29.   
  30. return 0;   
  31. }  

Virutual Function 实现动态绑定    

原创粉丝点击