• C++-PTA-动物世界


    补充程序 :

    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、补充主函数的问号部分,并运行程序,检查输出是否合理。

    #include <iostream>
    using namespace std;
    enum COLOR{ WHITE, RED, BROWN, BLACK, KHAKI };
    
    class Mammal
    {
    private:
        
    public:
            //constructors
        Mammal(){}
        Mammal(int age):itsAge(age){}
        ~Mammal(){}
            
            //accessors
        int GetAge()        const{return itsAge;}
        void SetAge(int age)     {itsAge = age;}
        int GetWeight()      {return itsWeight;}
        void SetWeight(int weight)     {itsWeight=weight;}
            
            //Other methods
        void Speak()    const
        {cout<<"Mammal is speaking..."<<endl;}
        void Sleep() const;
    protected:
        int itsAge;
        int itsWeight;
    };
    class Dog:public Mammal
    {
    private:
        COLOR itsColor;
    public:
        Dog(){}
        //    {itsAge=1;itsWeight=10;}
        Dog(int age):Mammal(age)
        {}
        Dog(int age, int w):Mammal(age)
        {itsWeight=w;}
        Dog(int age, COLOR color):Mammal(age)
        {color=WHITE;}
        Dog(int age, int w,COLOR color):Mammal(age)
        {itsColor=color;itsWeight=w;}
        ~Dog(){}
        COLOR GetColor()    {return itsColor;}
        void SetColor(COLOR color)  {itsColor=color;}
        void WagTail()  {cout<<"The dog is wagging its tail..."<<endl;}
        void BegForFood() ;
    };
    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 " << Yorkie.GetAge() << " years old." << endl;
        cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds." << endl;
        return 0;
    }

    不知道为什么输出结果不对……

    -----------

    2020-05-18 已更正

  • 相关阅读:
    AccessTokenValidation3 源码分析 jwttoken验证流程图
    .net core dump分析
    9102年了,汇总下HttpClient问题,封印一个
    Asp.net 性能监控之压测接口“卡住” 分析
    IdentityServer4源码颁发token分析及性能优化
    博客园设置看板娘和看板猫
    C# winform websocket学习笔记(三)HTML客户端
    C# winform websocket学习笔记(三)winform客户端
    C# winform websocket学习笔记(二)winform服务端
    C# winform websocket学习笔记(一)准备
  • 原文地址:https://www.cnblogs.com/loglian/p/12803258.html
Copyright © 2020-2023  润新知