问题及代码:
Problem C: A代码完善--哺乳动物
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 151 Solved: 125
[ Submit][ Status][ Web Board]
Description
注:本题只需要提交填写部分的代码,请按照C++方式提交。
根据类 Mammal派生Cattle 和Horse ,输出动物的叫声。
#include <stdio.h>
#include <iostream>
using namespace std;
class Mammal
{
public:
virtual void Speak() const=0;
virtual ~Mammal() {}
};
/*
请在该部分补充缺少的代码
*/
int main()
{
Mammal *p;
p=new Cattle;
p->Speak(); //输出牛的叫声moo!
delete p;
p=new Horse;
p->Speak(); //输出马的叫声neigh!
delete p;
return 0;
}
Input
无
Output
动物的叫声
Sample Output
moo!
neigh!
HINT
#include <stdio.h>
#include <iostream>
using namespace std;
class Mammal
{
public:
virtual void Speak() const=0;
virtual ~Mammal() {}
};
class Cattle:public Mammal
{
public:
void Speak() const
{
cout<<"moo!"<<endl;
}
};
class Horse:public Mammal
{
public:
void Speak() const
{
cout<<"neigh!"<<endl;
}
};
int main()
{
Mammal *p;
p=new Cattle;
p->Speak(); //输出牛的叫声moo!
delete p;
p=new Horse;
p->Speak(); //输出马的叫声neigh!
delete p;
return 0;
}