C++学习记录--day6

思维导图:

编程题:

#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;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值