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

相关推荐
珹洺37 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
古月-一个C++方向的小白7 小时前
C++11之lambda表达式与包装器
开发语言·c++
tanyongxi669 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
斯是 陋室11 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷12 小时前
C++:list
开发语言·c++
HHRL-yx12 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip
tomato0912 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
每一天都要努力^15 小时前
C++拷贝构造
开发语言·c++
NoirSeeker16 小时前
在windows平台上基于OpenHarmony sdk编译三方库并暴露给ArkTS使用(详细)
c++·windows·arkts·鸿蒙·交叉编译
落羽的落羽16 小时前
【C++】(万字)一文看懂“类与对象”
c++