- #include
-
- using namespace std;
-
- class Animal
- {
- private:
- string name;
- string variety; //品种
- public:
- Animal(){}
- Animal(string n, string v):name(n),variety(v)
- {}
- virtual void perform() = 0;
- void show()
- {
- cout << name << "是一头" << variety;
- }
- };
-
- class Elephant:public Animal
- {
- private:
- char sex;
- public:
- Elephant(){}
- Elephant(string n, string v, char s):Animal(n, v),sex(s)
- {}
- void perform()
- {
- show();
- cout << "喜欢一脚踩死一个人" << endl;
- }
- };
-
- class Lion:public Animal
- {
- private:
- char sex;
- public:
- Lion(){}
- Lion(string n, string v, char s):Animal(n, v),sex(s)
- {}
- void perform()
- {
- show();
- cout << "一顿只吃一个人" << endl;
- }
- };
-
- class Alligator:public Animal
- {
- private:
- char sex;
- public:
- Alligator(){}
- Alligator(string n, string v, char s):Animal(n, v),sex(s)
- {}
- void perform()
- {
- show();
- cout << "更喜欢死亡翻滚把人拆开再吃" << endl;
- }
- };
- int main()
- {
- Elephant e("长鼻子", "非洲象", 'F');
- Lion l("辛巴", "美洲狮", 'M');
- Alligator a("尼古拉斯", "尼罗鳄", 'M');
- Animal *p = &e;
- p->perform();
- p = &l;
- p->perform();
- p = &a;
- p->perform();
- return 0;
- }