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();
}

相关推荐
从零开始的代码生活_1 小时前
C++ 内存管理:从内存分区到 new/delete 底层原理
开发语言·c++·后端
aaPIXa6222 小时前
C++ 用 13 条规则让模型写出安全现代 C++
java·c++·安全
枕星而眠3 小时前
【数据结构】红黑树入门指南
运维·数据结构·c++·后端
2601_949817723 小时前
C++指针与引用深度精讲:底层原理、差异对比与高阶实战陷阱
java·jvm·c++
烟锁池塘柳04 小时前
【C/C++】解决C++控制台输出中文乱码问题
c语言·开发语言·c++
zh路西法5 小时前
【10天速通Navigation2】(九):LQR最优控制器的原理推导与Nav2插件实现
c++·ros2·最优控制·lqr·navigation2
粘稠的浆糊5 小时前
[AtCoder - abc465_d ]X to Y题解
数据结构·c++·算法
誰能久伴不乏5 小时前
C++11 随机数生成——告别 rand()
开发语言·c++
KouFiee5 小时前
同名隐藏基础
开发语言·c++
从零开始的代码生活_6 小时前
C++ string 详解:常用接口、字符串算法与深拷贝实现
开发语言·c++·后端·学习·算法