pta问题 虚函数plus C++

补充完整有关类和函数ShowAnimal的定义,使得主函数能正确的执行。

裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:

#include <iostream>
#include <string.h>
using namespace std;
class Animal{
    int speed;
    float jpheight;
    char food[21];         //长度不超过十个汉字,这里可以使用数组,不必用指针 
public:
    Animal(int s, float h, char f[]=""){
        speed = s; jpheight = h;
        strcpy(food,f);
    }

    virtual void Show()=0;
    void Set(int s, float h, char f[]=""){
        speed = s; jpheight = h;
        strcpy(food,f);
    }
    friend ostream& operator<<(ostream& o, Animal& a);
};
ostream& operator<<(ostream& o, Animal& a){
    o<<a.speed<<" "<<a.jpheight<<" "<<a.food;
    return o;
}
/* 请在这里填写答案 */



int main(){
    Cat mao;                            //定义对象mao 
    mao.Set();                            //输入mao的参数 
    Dog gou(8, 1.3, "筒子骨", 30);        //定义对象狗并初始化
    
    ShowAnimal(mao);
    ShowAnimal(gou);
}

输入样例:
输入mao对象的参数:速度、跳跃高度、食物和夜视范围。(食物描述汉字不超过10个)

9 2.9 鲫鱼 21
输出样例:
在这里给出相应的输出。例如:

9 2.9 鲫鱼 21
8 1.3 筒子骨 30

class Cat : public Animal 
{
public:
    Cat() : Animal(0, 0, (char*)""), night(0) {}
    void Show()
    {
        cout << *this << " " << night << endl;
    }
    void Set()
    {
        int s;
        float h;
        char f[21];
        int n;
        cin >> s >> h >> f >> n;
        Animal::Set(s, h, f);
        night = n;
    }
private:
    int night;
};

class Dog : public Animal
{
public:
    Dog(int s, float h, char f[], int n) : Animal(s, h, f), night(n) {}
    void Show()
    {
        cout << *this << " " << night << endl;
    }
private:
    int night;
};

void ShowAnimal(Animal& a)
{
    a.Show();
}

本文有引用gpt的相关内容

class Cat : public Animal{
public:
    Cat() : Animal(0, 0, ""){}
    void Show(){
        cout << speed << " " << jpheight << " " << food << endl;
    }
    void Set(){
        int s, n;
        float h;
        char f[21];
        cin >> s >> h >> f >> n;
        Animal::Set(s, h, f);
        night = n;
    }
private:
    int night;
};

class Dog : public Animal{
public:
    Dog(int s, float h, char f[], int n) : Animal(s, h, f), night(n){}
    void Show(){
        cout << speed << " " << jpheight << " " << food << " " << night << endl;
    }
private:
    int night;
};

void ShowAnimal(Animal& a){
    cout << a << " ";
    a.Show();
}