1.
#include <iostream>
using namespace std;
class Animal
{
private:
string name;
public:
Animal(){}
Animal(string name):name(name)
{cout << "animal" << endl;}
virtual void perfrom()
{
cout << "实现不同表演行为" << endl;
}
};
class Lion:public Animal
{
private:
int age;
public:
Lion(){}
Lion(string name,int age):Animal(name),age(age)
{
cout << "lion进行表演:" << endl;
}
void perfrom()
{
cout << "狮子叫" << endl;
}
};
class Elephant:public Animal
{
private:
int age;
public:
Elephant(){}
Elephant(string name,int age):Animal(name),age(age)
{
cout << "elephant进行表演:" << endl;
}
void perfrom()
{
cout << "大象跳舞" << endl;
}
};
class Monkey:public Animal
{
private:
int age;
public:
Monkey(){}
Monkey(string name,int age):Animal(name),age(age)
{
cout << "monkey进行表演:" << endl;
}
void perfrom()
{
cout << "猴子骑车" << endl;
}
};
int main()
{
Animal *a = new Lion("辛巴",6);
a->perfrom();
Animal *a2 = new Elephant("库迪",8);
a2->perfrom();
Animal *a3 = new Monkey("悟空",100);
a3->perfrom();
return 0;
}
思维导图: