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

相关推荐
李元豪3 小时前
【智鹿空间】c++实现了一个简单的链表数据结构 MyList,其中包含基本的 Get 和 Modify 操作,
数据结构·c++·链表
UestcXiye3 小时前
《TCP/IP网络编程》学习笔记 | Chapter 9:套接字的多种可选项
c++·计算机网络·ip·tcp
一丝晨光4 小时前
编译器、IDE对C/C++新标准的支持
c语言·开发语言·c++·ide·msvc·visual studio·gcc
丶Darling.5 小时前
Day40 | 动态规划 :完全背包应用 组合总和IV(类比爬楼梯)
c++·算法·动态规划·记忆化搜索·回溯
奶味少女酱~5 小时前
常用的c++特性-->day02
开发语言·c++·算法
我是哈哈hh6 小时前
专题十八_动态规划_斐波那契数列模型_路径问题_算法专题详细总结
c++·算法·动态规划
_小柏_7 小时前
C/C++基础知识复习(15)
c语言·c++
_oP_i7 小时前
cmake could not find a package configuration file provided by “Microsoft.GSL“
c++
mingshili7 小时前
[python] 如何debug python脚本中C++后端的core dump
c++·python·debug
PaLu-LI8 小时前
ORB-SLAM2源码学习:Frame.cc: Frame::isInFrustum 判断地图点是否在当前帧的视野范围内
c++·人工智能·opencv·学习·算法·ubuntu·计算机视觉