多态之动态绑定的基础——虚函数

 

#include <iostream>
 using namespace std;
 class base
 {
 public:
     void func(const char* str){cout << "base: " << str << endl;}
     virtual void func(int num){cout << "base: " << num << endl;}
 };
 class derived : public base
 {
 public:
     void func(const char* str){cout << "derived: " << str << endl;}
     void func(int num){cout << "derived: " << num << endl;}
     void func(){cout << "the function is in derived!" << endl;}
 };
 int main()
 {
   base *pb;
   derived objd;
   pb = objd;
   pb->func(11);
   pb->func("hello");
   // pb->func();
   getchar();
   return 0;
 }

 

derived: 11
base: hello

 

posted on 2012-06-19 17:26  Joe_Lui  阅读(96)  评论(0)    收藏  举报

导航