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

相关推荐
悠仁さん2 小时前
【C++】AVL 树
c++
wuminyu2 小时前
JVM规范学习总结:解释器与JIT编译器如何处理JVM字节码
java·linux·c语言·jvm·c++
小徐不徐说2 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang3 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
充值内卷4 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
樱花树下的数学喵7 小时前
手写简易 STL string|C++ 字符串底层实现
开发语言·c++
AI吃大瓜7 小时前
指针压力表读数3:C/C++实现指针压力表读数(可实时检测)
c++·指针读数·指针压力表·压力表自动读数·压力表读数
-银雾鸢尾-7 小时前
两数之和-C++实现
c++·算法
j7~7 小时前
【动态规划算法专题】动态规划五部曲实战:泰波那契数、三步问题、最小花费爬楼梯
c++·算法·动态规划·数据结构与算法
jimy17 小时前
C++ 报错的 qualifiers 的意思
开发语言·c++