模拟跟随导演去动物园:
#include <iostream>using namespace std;
class Animal
{
private:string name;
public:Animal(){}Animal(string name):name(name){}virtual void perform(){cout << name << "为我们讲解,说:" << endl;}
};
class lion:public Animal
{
private:string name;string peculiarity;
public:lion(){}lion(string name,string n,string peculiarity):Animal(name),name(n),peculiarity(peculiarity){}void perform(){Animal::perform();cout << name << "正在睡觉" << endl;cout << "特点:" << peculiarity <<endl;}
};
class Monkey:public Animal
{
private:string name;string peculiarity;
public:Monkey(){}Monkey(string name,string n,string peculiarity):Animal(name),name(n),peculiarity(peculiarity){}void perform(){Animal::perform();cout << name << "正在打招呼" << endl;cout << "特点:" << peculiarity <<endl;}
};
int main()
{Animal *a;lion l("刘玉文","狮子","体型大,躯体均匀,四肢中长,趾行性。 头大而圆,吻部较短,视、听、嗅觉均很发达。");Monkey m("刘玉文","猴子","有爪的哺乳类动物;有胎盘的动物;有较高的眼眶;有发达的盲肠;胸部有二个乳房;有三种牙齿和脱落更新的恒齿;姆指与其他指头呈相反的位置;脑壳有一钙质的裂缝。");a=&l;a->perform();a=&m;a->perform();return 0;
}