刘华世的官方博客

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;
}
posted @ 2012-11-09 17:14  pythonschool  阅读(290)  评论(0编辑  收藏  举报
刘华世的官方博客