動物世界


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、補充主函數的問號部分,並運行程序,檢查輸出是否合理。

  1 #include<iostream>
  2 using namespace std;
  3 enum COLOR{ WHITE, RED, BROWN, BLACK, KHAKI };
  4 class Mammal
  5 {
  6   public:
  7     //constructors
  8     Mammal();
  9     Mammal(int age);
 10     ~Mammal();
 11     
 12     //accessors
 13     int GetAge() const;
 14     void SetAge(int);
 15     int GetWeight() const;
 16     void SetWeight(int);
 17     
 18     //Other methods 
 19     void Speak() const;
 20     void Sleep() const;   
 21   protected:
 22     int itsAge;
 23     int itsWeight;
 24 };
 25 Mammal::Mammal(){}//定義構造函數
 26 Mammal::Mammal(int age):itsAge(age){}
 27 Mammal::~Mammal(){}
 28 int Mammal::GetAge() const//用於獲取itsAge
 29 {
 30   return itsAge;
 31 }
 32 void Mammal::SetAge(int age)//用於設置itsAge
 33 {
 34   itsAge=age;
 35 }
 36 int Mammal::GetWeight() const//用於獲取itsWeight
 37 {
 38   return itsWeight;
 39 }
 40 void Mammal::SetWeight(int weight)//用於設置itsWeight
 41 {
 42   itsWeight=weight;
 43 }
 44 void Mammal::Speak() const//成員函數
 45 {
 46   cout<<"Mammal is speaking..."<<endl;
 47 }
 48 void Mammal::Sleep() const//成員函數
 49 {
 50   cout<<"Mammal is sleeping..."<<endl;
 51 }
 52 
 53 class Dog:public Mammal//定義Dog類,繼承Mammal類
 54 {
 55 private:
 56   COLOR itsColor;//設置顏色為私有屬性,類型為枚舉COLOR類型
 57 public:
 58   Dog();
 59   Dog(int age);
 60   Dog(int age,int weight);
 61   Dog(int age,COLOR color);
 62   Dog(int age,int weight,COLOR color);
 63   ~Dog();
 64   COLOR GetColor();
 65   void SetColor(COLOR color);
 66   void WagTail();
 67   void BegForFood();
 68 };
 69 Dog::Dog(){}//定義構造函數
 70 Dog::Dog(int age):Mammal(age){}//調用基類的構造函數
 71 Dog::Dog(int age,int weight):Mammal(age)
 72 {
 73   this->SetWeight(weight);
 74 }
 75 Dog::Dog(int age,COLOR color):Mammal(age)
 76 {
 77   this->itsColor=color;
 78 }
 79 Dog::Dog(int age,int weight,COLOR color):Mammal(age)
 80 {
 81   this->SetWeight(weight);
 82   this->itsColor=color;
 83 }
 84 Dog::~Dog(){}//定義析構函數
 85 COLOR Dog::GetColor()//獲取對象的itsColor值
 86 {
 87   return itsColor;
 88 }
 89 void Dog::SetColor(COLOR color)//設置對象的itsCokor值
 90 {
 91   itsColor=color;
 92 }
 93 void Dog::WagTail()//定義成員函數
 94 {
 95   cout<<"The dog is wagging its tail..."<<endl;
 96 }
 97 void Dog::BegForFood()//定義成員函數
 98 {
 99   cout<<"The dog is begging its food..."<<endl;
100 }
101 int main()
102 {
103   Dog Fido;
104   Dog Rover(5);
105   Dog Buster(6, 8);
106   Dog Yorkie(3, RED);
107   Dog Dobbie(4, 20, KHAKI);
108   Fido.Speak();
109   Rover.WagTail();
110   cout<<"Yorkie is "<<Yorkie.GetAge()<<" years old."<<endl;
111   cout<<"Dobbie weighs "<<Dobbie.GetWeight()<<" pounds."<<endl;   
112   return 0;
113 }

 構造函數和析構函數只聲明不定義會報錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM