Day 6 C++

cpp 复制代码
#include <iostream>
//不同种类的动物,如狮子、大象、猴子等。现在,动物园里有一位讲解员,他会为每种动物表演做简单的介
//绍。定义一个基类 Animal,其中有一个虛函数perform(),用于在子类中实现不同的表演行为
using namespace std;
class Animal
{
private:
    string name;
    int weight;
    string color;
public:
    Animal(){}
    Animal(string name,int w,string c):name(name),weight(w),color(c)
    {}
   virtual  void perform()
    {
       cout << "动物讲解开始......" << endl ;
    }
    void show()
    {
        cout << name << " " << weight << " " << color << " " <<endl;
    }
     void over()
     {
         cout << "动物讲解结束......" << endl ;
     }
};


class Lion :public Animal
{
private:
    string sound;
    int age;
public:
    Lion(){}
    Lion(string sound,int age,string name,int weight,string color):Animal(name,weight,color),sound(sound),age(age)
    {}
    void perform()
    {
        cout << "狮子喷火......." << endl;
    }
    void show()
    {
        cout << sound << " " << age << " " ;
    }
};


class Elephant  :public Animal
{
private:
    string nose;
    int high;
public:
    Elephant(){}
    Elephant(string nose,int h,string name,int weight, string color):Animal(name,weight,color),nose(nose),high(h)
    {}
    void perform()
    {
        cout << "大象跳圈圈......." << endl;
    }
    void show()
    {
        cout << nose << " " << high << " ";

    }
};


class Monkey  :public Animal
{
private:
    string breed;
    int birth;
public:
    Monkey(){}
    Monkey(string breed,int birth,string name,int weight, string color):Animal(name,weight,color),breed(breed),birth(birth)
    {}
    void perform()
    {
        cout << "猴子蹦极......." << endl;
    }
    void show()
    {
        cout << breed << " "<< birth << " " ;

    }
};


int main()
{     Animal f;
      f.perform();
      Animal *p;
      Lion A("怒吼",5,"狮子",300,"棕色");
      Elephant B("大长鼻子",20,"大象",500,"灰色");
      Monkey C("金丝猴",2023,"猴子",36,"金色");
      cout << "________________________" << endl;
      cout << endl;
      A.show();
      A.Animal::show();
      p=&A;
      p->perform();
      cout << endl;
      cout << endl;
      B.show();
      B.Animal::show();
      p=&B;
      p->perform();
      cout << endl;
      cout << endl;
      C.show();
      C.Animal::show();
      p=&C;
      p->perform();
      cout << endl;
      cout << "________________________" << endl;
     f.over();
}

相关推荐
ziguo11222 小时前
深入浅出 C/C++ 数据类型:从入门到踩坑
linux·c语言·c++·windows·visual studio
白色冰激凌3 小时前
[SECS/GEM研究] (三)SECS-I 串口上消息怎么分块和重传
c++·secs/gem
光头闪亮亮3 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(中)
android·c++·go
光头闪亮亮3 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(下)
android·c++·go
_wyt0014 小时前
洛谷 P7912 [CSP-J 2021] 小熊的果篮 题解
c++·队列
choumin5 小时前
创建型模式——原型模式
c++·设计模式·原型模式·创建型模式
code_pgf5 小时前
C/C++ 常用容器功能汇总
c语言·开发语言·c++
王维同学6 小时前
Credential Provider、Filter 与 PLAP 的 CLSID 枚举
c++·windows·安全·注册表
Carlgood-Minecraft7 小时前
随机分位系统破解版 (BPS)Version-B4.2
c++
choumin7 小时前
结构型模式——装饰模式
c++·设计模式·装饰模式·结构型模式