Problem C: 让动物们叫起来吧!

Description

Tom家里养了很多动物,比如有鸭子、火鸡和公鸡。它们的叫声都不相同。现在,请编写类Animal、Cock、Turkey和Duck,根据给出的main()函数及样例分析每个类的属性、行为及相互关系,以模仿Tom家的情况。

提示:动物们都有自己的名字。

Input

输入有多行。第一行正整数M表示之后有M个测试用例,每个测试用例包括2部分:前一部分是动物的名字,后一部分是动物的类型(用A、B、C分别表示鸭子、火鸡和公鸡)。

Output

输出有M行,每个测试用例对应一样。见样例。

Sample Input

3Baby CRubby BTobby A

Sample Output

Baby is a cock, and it can crow.Rubby is a turkey, and it can gobble.Tobby is a duck, and it can quack.

HINT



#include <bits/stdc++.h>
using namespace std;
class Animal{
 
  public:
      string name;
      Animal(string s):name(s){}
      virtual void sound()=0;
};
class Cock:public Animal{
   string name1;
   public:
       Cock(string s):Animal(s){
         cout<<s<<" is a cock,";
       }
       void sound(){
         cout<<" and it can crow."<<endl;
       }
};
class Turkey:public Animal{
   string name2;
   public:
       Turkey(string s):Animal(s){
         cout<<s<<" is a turkey,";
       }
       void sound(){
         cout<<" and it can gobble."<<endl;
       }
};
class Duck:public Animal{
   string name3;
   public:
       Duck(string s):Animal(s){
         cout<<s<<" is a duck,";
       }
       void sound(){
         cout<<" and it can quack."<<endl;
       }
};
 
int main()
{
    int cases;
    string name;
    char type;
    Animal *animal;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
       cin>>name>>type;
       switch(type)
       {
        case 'A':
            animal = new Duck(name);
            break;
        case 'B':
            animal = new Turkey(name);
            break;
        case 'C':
            animal = new Cock(name);
            break;
       }
       animal->sound();
    }
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值