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;
}
Accepted Code
/**
* 2020年6月9日15:46:49
* OJ计算机科学与技术2019-3,4:程序设计基础(2-2)—实验10
*/
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
class Animal {
protected:
string name;
public:
Animal(string n = "") : name(n) {}
virtual ~Animal() {}
virtual void sound() = 0;
};
class Duck : public Animal {
public:
Duck(string n) : Animal(n) {}
void sound() {
cout << name << " is a duck, and it can quack.\n";
}
};
class Turkey : public Animal {
public:
Turkey(string n) : Animal(n) {}
void sound() {
cout << name << " is a turkey, and it can gobble.\n";
}
};
class Cock : public Animal {
public:
Cock(string n) : Animal(n) {}
void sound() {
cout << name << " is a cock, and it can crow.\n";
}
};
本文介绍了一个使用C++实现的程序设计案例,通过继承和多态特性,模拟了不同动物(如鸭子、火鸡和公鸡)的声音。程序通过读取输入的名字和类型,创建相应的动物对象,并调用其声音方法,展示了面向对象编程的基本概念。
736

被折叠的 条评论
为什么被折叠?



