1.定义一个基类 Animal,其中有一个虛函数perform(),用于在子类中实现不同的表演行为。
#include <iostream>using namespace std;
class Animal
{
private:int weight;
public:Animal(){}Animal(int weight):weight(weight){}virtual void perform() = 0;virtual ~Animal(){}
};
class Cat:public Animal
{
private:string name;
public:Cat(){}Cat(int weight,string name):Animal(weight),name(name){}void perform(){cout << "后空翻" << endl;}~Cat(){}
};
class Lion:public Animal
{
private:string name;
public:Lion(){}Lion(int weight,string name):Animal(weight),name(name){}void perform(){cout << "睡觉" << endl;}~Lion(){}
};int main()
{Cat c(5,"小咪");Lion l(55,"大黄");Animal *a;a = &c;a->perform();a = &l;a->perform();return 0;
}
2.思维导图