C++的简单问题(我是菜鸟)
#include<iostream>
#include<string>
using namespace std;
class Animal
{
public:
string voice;
};
class Cat:public Animal
{
public:
void sounds();
};
void Cat::sounds()
{
voice="meow";
cout<<voice<<endl;
}
class Dog:public Animal
{
public:
void sounds();
};
void Dog::sounds()
{voice="wang";
cout<<voice<<endl;
}
class Tiger:public Animal
{
public:
void sounds();
};
void Tiger::sounds()
{
voice="howl";
cout<<voice<<endl;
}
int main(){
string a;
cin>>a;
while(a!=" "
{
if(a=="cat"
{
Cat cat;
cat.sounds();
}else if(a=="dog"
{
Dog dog;
dog.sounds();
}
else if(a=="tiger"
{
Tiger tiger;
tiger.sounds();
}else if(a!="cat"||a!="dog"||a!="tiger"
{
break;
}
cin>>a;
}
return 0;
}
这个简单程序如何终止while循环,测试数据为
cat
dog
tiger
dog
输出:
meow
wang
howl
wang
原题是:定义一个Animal基类,此基类有三个继承类,分别是Cat、Dog、Tiger,基类与三个继承类都有voice函数用于发声,Animal发声为~,Cat发声为meow,Dog发声为wang,Tiger发声为howl。补全类定义,并写一个函数,要求传入三个不同的动物类的实例后会输出不同的叫声。有若干行输入,每行是cat、dog、tiger之一,输出是他们相对应的叫声.
谢谢!或者把您做出来的代码贴出来参考一下
返回小木虫查看更多
京公网安备 11010802022153号
你的程序不是已经写完了吗?你还有什么问题?程序不能运行的话,就贴出错误信息
能运行,我把题目意思搞错了,这个东西是对的!谢谢回复,分都给你了
,