标签:
1 #include <iostream.h> 2 3 class A 4 { 5 public: 6 void f1() 7 { cout << "a" << endl; } 8 virtual void f2() 9 { cout << "b" << endl; } 10 }; 11 12 class B : public A 13 { 14 public: 15 void f1() 16 { cout << "c" << endl; } 17 virtual void f2() 18 { cout << "d" << endl; } 19 }; 20 21 void main() 22 { 23 A a; 24 B b; 25 a.f1(); 26 a.f2(); 27 b.f1(); 28 cout << "--------------------" << endl; 29 30 A *pa = &b; 31 pa->f1();//a 32 pa->f2();//d 33 cout << "-------------------" << endl; 34 35 A *fa = (A *)&b; 36 fa->f1();//a 37 fa->f2();//d 38 cout << "-------------------" << endl; 39 }
标签:
原文地址:http://www.cnblogs.com/lweleven/p/virtualfunc.html