特级茶叶 2022-11-04 13:23 采纳率: 78.6%
浏览 102
已结题

用C++编写相关程序4

请编写程序,上机调试,题目如下图,请按题目要求编写,并给出程序

img

img

  • 写回答

3条回答 默认 最新

  • 游一游走一走 2022-11-04 14:42
    关注
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    class Animal
    {
    private:
        string nameID;
        string order;
        string family;
        string genus;
    
    public:
        Animal(string nameID, string order, string family, string genus) : nameID(nameID), order(order), family(family), genus(family) {}
        string to_string()
        {
            return nameID + ":" + order + "、" + family + "、" + genus;
        }
        virtual void who()
        {
            cout << this->nameID << endl;
        }
        virtual void sound() = 0;
    };
    class Sheep : public Animal //继承
    {
    public:
        Sheep(string nameID, string order, string family, string genus) : Animal(nameID, order, family, genus) {}
        void who()
        {
            cout << "Sheep!" << endl;
        }
        void sound()
        {
            cout << "miemie!" << endl;
        }
    };
    class Dog : public Animal //继承
    {
    public:
        Dog(string nameID, string order, string family, string genus) : Animal(nameID, order, family, genus) {}
        void who()
        {
            cout << "Dog!" << endl;
        }
        void sound()
        {
            cout << "wangwang!" << endl;
        }
    };
    class Cow : public Animal //继承
    {
    public:
        Cow(string nameID, string order, string family, string genus) : Animal(nameID, order, family, genus) {}
        void who()
        {
            cout << "Cow!" << endl;
        }
        void sound()
        {
            cout << "EnEn!" << endl;
        }
    };
    class Zoo
    {
    private:
        Animal *animals[50];
        int size;
    
    public:
        Zoo() { size = 0; }
        void showAnimals()
        {
            int sheep = 0, dog = 0, cow = 0;
            for (int i = 0; i < this->size; i++)
            {
                Animal *temp = animals[i];
                temp->who();
                cout << temp->to_string() << endl;
                temp->sound();
                if (typeid(*temp) == typeid(Sheep))
                {
                    sheep++;
                }
                if (typeid(*temp) == typeid(Dog))
                {
                    dog++;
                }
                if (typeid(*temp) == typeid(Cow))
                {
                    cow++;
                }
            }
            cout << "羊:" << sheep << "狗:" << dog << "牛:" << cow << endl;
        }
        void addAnimal(Animal *p)
        {
            if (size < 50)
                animals[size++] = p;
        }
    };
    int main()
    {
        Zoo zoo;
        int size = 0;
        Animal *temp;
        while (size++ < 30)
        {
            int index = rand() % 3 + 1;
            switch (index)
            {
            case 1:
                temp = new Sheep("绵羊", "偶蹄目", "牛科", "绵羊属");
                break;
            case 2:
                temp = new Dog("狗", "食肉目", "犬科", "犬属");
                break;
            case 3:
                temp = new Cow("牛", "偶蹄目", "牛科", "牛属");
                ;
                break;
    
            default:
                break;
            }
            zoo.addAnimal(temp);
        }
        zoo.showAnimals();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月12日
  • 已采纳回答 11月4日
  • 创建了问题 11月4日

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本