1.思维导图
2.定义一个基类Animal,其中有一个虚函数perform (),用于在子类中实现不同的表演行为。
#include <iostream>using namespace std;
class Animal
{
private:public:void virtual perform() = 0;};
class Tiger:public Animal
{
private:string name;
public:Tiger(string name):name(name){}void perform(){cout << name << endl;cout << "是哺乳纲的大型猫科动物;毛色浅黄或棕黄色,满身黑色横纹;头圆、耳短,耳背面黑色,中央有一白斑甚显著;四肢健壮有力;尾粗长,具黑色环纹,尾端黑色。" << endl;}
};
class Monkey:public Animal
{
private:string name;
public:Monkey(string name):name(name){}void perform(){cout << name << endl;cout << "是灵长目猴科仰鼻猴属的动物统称,因身体长着柔软的金色长毛得名" << endl;}
};
class Panda:public Animal
{
private:string name;
public:Panda(){}Panda(string name):name(name){}void perform(){cout << name << endl;cout << "属于熊科、大熊猫属哺乳动物,是中国特有种。大熊猫仅有两个亚种,四川亚种和秦岭亚种 ,主要栖息地是中国四川、陕西和甘肃的山区。" << endl;}
};
int main()
{Tiger s1("虎");Monkey s2("金丝猴");Panda s3("大熊猫");Animal* p;p=&s1;p->perform();p=&s2;p->perform();p=&s3;p->perform();return 0;
}