1 #include<iostream> 2 using namespace std; 3 class BaseClass 4 { 5 public: 6 virtual void fn1(); 7 void fn2 (); 8 }; 9 void BaseClass::fn1() 10 { 11 //cout<<"调用基类的虚函数fn1()"<<endl; 12 } 13 void BaseClass::fn2() 14 { 15 cout<<"调用基类的虚函数fn2()"<<endl; 16 } 17 class DerivedClass:public BaseClass 18 { 19 public: 20 void fn1(); 21 void fn2(); 22 }; 23 void DerivedClass::fn1() 24 { 25 cout<<"调用派生类的函数fn1()"<<endl; 26 } 27 void DerivedClass::fn2() 28 { 29 cout<<"调用派生类的函数fn2()"<<endl; 30 } 31 int main() 32 { 33 DerivedClass aDerivedClass; 34 DerivedClass*pDerivedClass=&aDerivedClass; 35 BaseClass*pBaseClass=&aDerivedClass; 36 pBaseClass->fn1(); 37 pBaseClass->fn2(); 38 pDerivedClass->fn1(); 39 pDerivedClass->fn2(); 40 }
标签:pDerivedClass,fn2,fn1,5.9,void,BaseClass From: https://www.cnblogs.com/lijianlongCode13/p/17389004.html