随笔 - 25,  文章 - 0,  评论 - 0,  阅读 - 2424

一.问题描述:

补充程序 :

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

二.代码实现:

复制代码
#include<iostream>
using namespace std;

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

class Mammal
{
public:
    //constructors
    Mammal() {};
    Mammal(int age,int weight) :itsAge(age),itsWeight(weight){};
    ~Mammal() {};

    //accessors
    int GetAge() const { return itsAge; };
    void SetAge(int age) { itsAge = age; };
    int GetWeight() const { return itsWeight; };
    void SetWeight(int weight) { itsWeight = weight; };

    //Other methods
void Speak() const {
    cout<<"Mammal is speaking..."<<endl;
}
void Sleep() const {
    cout<<"Mammal is sleeping..."<<endl;
}
protected:
    int itsAge;
    int itsWeight;
};

class Dog :public Mammal {

public:
    Dog() {};
    Dog(int itsAge) :Mammal(itsAge, itsWeight) {};
    Dog(int itsAge, int itsWeight) :Mammal(itsAge, itsWeight) {};
    Dog(int itsAge, COLOR color) :Mammal(itsAge, itsWeight), itsColor(color) {};
    Dog(int itsAge, int itsWeight, COLOR color) :Mammal(itsAge, itsWeight), itsColor(color){};

    COLOR GetColor() { return itsColor; }
    void SetColor(COLOR color) { itsColor = color; }

        void WagTail() {
            cout<<"The dog is wagging its tail..."<<endl;
        }
    void BegForFood(){
            cout<<"The dog is begging its food..."<<endl;
        }


protected:
    COLOR itsColor;
};
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;
}
复制代码

 

 
posted on   标志蛋挞  阅读(63)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示