C++语言 对动物的行为实现多态
//#C++语言 对动物的行为实现多态 #include <iostream> using namespace std; class CAnimal { public: virtual void Cry()=0;//纯虚函数 }; class CDog:public CAnimal { public: virtual void Cry() { cout << "wang wang! The dog sound!\n" << endl; } }; class CCat:public CAnimal { public: virtual void Cry() { cout << "miao miao! The cat sound!\n" << endl; } }; int main(int argc, char* argv[]) { /* CAnimal *p; CDog cdog; p = &cdog; p->Cry(); CCat ccat; p = &ccat; p->Cry(); */ CAnimal *animal; animal = new CDog; animal->Cry(); delete animal; animal = new CCat; animal->Cry(); delete animal; return 0; }
学习笔记转摘于:
丝酷网 http://www.pythonschool.com/