思维导图
练习题
1>定义一个基类 Animal,其中有一个虛函数perform(),用于在子类中实现不同的表演行为。
#include <iostream>using namespace std;class Animal
{
public:virtual void perform() {cout << "这是一个动物,但没有具体的表演" << endl;}
};class Lion:public Animal
{
public:void perform(){cout << "狮子正在叫!" << endl;}
};class Elephant:public Animal
{
public:void perform(){cout << "大象正在摇动长长的鼻子!" << endl;}
};class Monkey : public Animal
{
public:void perform(){cout << "猴子正在吃香蕉!" << endl;}
};int main() {Lion lion;Elephant elephant;Monkey monkey;Animal *animal1 = &lion;Animal *animal2 = &elephant;Animal *animal3 = &monkey;animal1->perform();animal2->perform();animal3->perform();return 0;
}