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

Posted 好奇怪的妹妹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Problem A: 让动物们叫起来吧!相关的知识,希望对你有一定的参考价值。

Description

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

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

Input

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

Output

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

Sample Input

3 Baby C Rubby B Tobby 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

Append Code

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;
}
 
代码
#include <iostream>
using namespace std;
 
class Animal
{
 
    public:
    Animal(){}
    virtual void sound(){}
};
class Cock:public Animal
{
    string name1;
public:
    Cock(string n):name1(n){}
    void sound()
    {
        cout<<name1<<" is a cock, and it can crow."<<endl;
    }
};
class Turkey:public Animal
{
    string name2;
public:
    Turkey(string  n):name2(n){}
    void sound()
    {
        cout<<name2<<" is a turkey, and it can gobble."<<endl;
    }
};
class Duck:public Animal
{
    string name3;
public:
    Duck(string n):name3(n){}
    void sound()
    {
        cout<<name3<<" is a duck, 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;
}

以上是关于Problem A: 让动物们叫起来吧!的主要内容,如果未能解决你的问题,请参考以下文章

Problem D: 动物爱好者

Problem E: 动物爱好者

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

IOS开发-OC学习-常用功能代码片段整理

食物链

codevs 1074 食物链