PTA 动物世界 (15 分)(继承)

博客围绕补充程序展开,需实现Mammal类方法,由其派生出Dog类并增加itsColor成员,为Dog类添加构造函数、访问器及其他方法,同时补充主函数内容并运行检查输出合理性,还给出了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

补充程序 :

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>
#include<cstring>
using namespace std;
class COLOR{
      char *color;
    public:
       COLOR(){
       	color=new char [20];
       	strcpy(color,"BLACK");
	   }
       COLOR(char *c){
           color=new char[20];
           strcpy(color,c);
       } 
};
class Mammal
{
	public:
		//constructors
		Mammal(){
			itsAge=0;
			itsWeight=0;
		}
		Mammal(int age){
            itsAge=age;
        }
        Mammal(int a,int w){
        	itsAge=a;
        	itsWeight=w;
		}
		//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;
        }
	protected:
		int itsAge;
		int itsWeight;
};
class Dog:public Mammal{
      COLOR color;
      public:
      Dog(){
	  }
      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){
	  }
      void WagTail(){
          cout<<"The dog is wagging its tail..."<<endl;
      }
};
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.";
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值