PTA 动物世界

176 阅读1分钟

补充程序 :

1、实现Mammal类的方法

2、由Mammal类派生出Dog类,在Dog类中增加itsColor成员(COLOR类型)

3、Dog类中增加以下方法:

constructors: Dog()、Dog(int age)、Dog(int age, int weight)、Dog(int age, COLOR color)、 Dog(int age, int weight, COLOR color)、~Dog()

accessors: GetColor()、SetColor()

Other methods: WagTail()、BegForFood()

并实现以上这些方法 。

提示:类似Speak()WagTail()这些动作,函数体可以是输出一句话。比如:Mammal is spaeking... , The Dog is Wagging its tail...

4、补充主函数的问号部分,并运行程序,检查输出是否合理。

enum COLOR{ WHITE, RED, BROWN, BLACK, KHAKI };

class Mammal
{
    public:
        //constructors
        Mammal();
        Mammal(int age);
        ~Mammal();

        //accessors
        int GetAge() const;
        void SetAge(int);
        int GetWeight() const;
        void SetWeight(int);

        //Other methods    
        void Speak() const;
        void Sleep() const;        
    protected:
        int itsAge;
        int itsWeight;
};

int main()
{
    Dog Fido;
    Dog Rover(5);
    Dog Buster(6, 8);
    Dog Yorkie(3, RED);
    Dog Dobbie(4, 20, KHAKI);
    Fido.Speak();
    Rover.WagTail();
    cout << "Yorkie is " << ?? << " years old." << endl;
    cout << "Dobbie    weighs " << ?? << " pounds." << endl;   
    return 0;
}

输入格式:

输出格式:

按照程序格式输出。

输入样例:

在这里给出一组输入。例如:

无
结尾无空行

输出样例:

在这里给出相应的输出。例如:

Mammal is speaking...
The dog is wagging its tail...
Yorkie is 3 years old.
Dobbie weighs 20 pounds.
结尾无空行

代码:

#include<iostream>
#include<cstring>
using namespace std;
class COLOR {
    char* color;
public:
    COLOR() {
        color = new char[20];
        strcpy(color, "BLACK");
    }
    explicit COLOR(char* c) {
        color = new char[20];
        strcpy(color, c);
    }
};
class Mammal
{
public:
    //constructors
    Mammal() {
        itsAge = 0;
        itsWeight = 0;
    }
    explicit Mammal(int age) {
        itsAge = age;
    }
    Mammal(int a, int w) {
        itsAge = a;
        itsWeight = w;
    }
    //accessors
    [[nodiscard]] int GetAge() const {
        return itsAge;
    }

    [[maybe_unused]] void SetAge(int age) {
        itsAge = age;
    }
    [[nodiscard]] int GetWeight() const {
        return itsWeight;
    }

    [[maybe_unused]] void SetWeight(int weight) {
        itsWeight = weight;
    }
    //Other methods	
    static void Speak() {
        cout << "Mammal is speaking..." << endl;
    }
protected:
    int itsAge;
    int itsWeight{};
};
class Dog :public Mammal {
    [[maybe_unused]] COLOR color;
public:
    Dog() = default;
    explicit Dog(int a) {
        itsAge = a;
    }
    Dog(int a, char* c) :color(c), Mammal(a) {
    }
    Dog(int a, int w) :Mammal(a, w) {
    }
    Dog(int a, int w, char* c) :color(c), Mammal(a, w) {
    }
    static void WagTail() {
        cout << "The dog is wagging its tail..." << endl;
    }
};
int main()
{
    [[maybe_unused]] Dog Fido;
    [[maybe_unused]] Dog Rover(5);
    [[maybe_unused]] Dog Buster(6, 8);
    Dog Yorkie(3, "RED");
    Dog Dobbie(4, 20, "KHAKI");
    Dog::Speak();
    Dog::WagTail();
    cout << "Yorkie is " << Yorkie.GetAge() << " years old." << endl;
    cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds.";
    return 0;
}

提交结果:

8.png