转自:http://edu.codepub.com/2011/0314/30061.php
#include <iostream> #include <string> using namespace std; class CAnimal { public: CAnimal(){cout<< "animal constructor" <<endl;} virtual ~CAnimal(){cout<< "animal destructor" <<endl;} virtual void speek() const{cout<< "animal speek" <<endl;} }; class CDog:public CAnimal { public: CDog(){ cout<< "dog constructor" <<endl;} virtual ~CDog(){cout<< "dog destructor" <<endl;} virtual void speek() const {cout<< "dog speek" <<endl;} void move() const {cout<< "dog move one step" <<endl;} }; class CCat:public CAnimal { public: CCat(){ cout<< "cat constructor" <<endl;} virtual ~CCat(){cout<< "cat destructor" <<endl;} virtual void speek() const {cout<< "cat speek" <<endl;} void run() const {cout<< "cat run one step" <<endl;} }; int main() { CAnimal *pBase = new CDog; static_cast<CDog*>(pBase)->move(); delete pBase; CAnimal *pBase2 = new CCat; static_cast<CCat*>(pBase2)->run(); delete pBase2; return 0; }