思维导图:
编程题:
#include <iostream> using namespace std; class Animal { protected: string name;//名称 string pecu;//特点 public: Animal(string name = "未知动物",string pecu = "未知"):name(name),pecu(pecu) { cout << "基类::构造函数" << endl; } ~Animal() { cout << "基类::析构函数" << endl; } virtual void perform() { cout << " 基类的perform" << endl; } }; class Lion:public Animal { public: Lion(string name = "未知",string pecu = "未知") :Animal(name,pecu) { cout << "Lion的构造函数" << endl; } ~Lion() { cout << "Lion的析构函数" << endl; } virtual void perform() { cout << "狮子正在跳火圈..." << endl; cout << "它的名字叫:" << this->name << endl << "特点是:" << this->pecu << endl; } }; class Monkey:public Animal { public: Monkey(string name = "未知",string pecu = "未知") :Animal(name,pecu) { cout << "Monkey的构造函数" << endl; } ~Monkey() { cout << "Monkey的析构函数" << endl; } virtual void perform() { cout << "猴子正在走钢丝..." << endl; cout << "它的名字叫:" << this->name << endl << "特点是:" << this->pecu << endl; } }; int main() { Monkey ani1("孙悟空","拿着两把西瓜刀从南天门砍到北天门不眨眼"); Lion ani2("狮驼岭大王","一口气吃十三个猪八戒不打嗝"); Animal *p1 = &ani1; Animal *p2 = &ani2; p1->perform(); p2->perform(); return 0; }