C++的簡(jiǎn)單問題(我是菜鳥)
#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;
}
這個(gè)簡(jiǎn)單程序如何終止while循環(huán),測(cè)試數(shù)據(jù)為
cat
dog
tiger
dog
輸出:
meow
wang
howl
wang
原題是:定義一個(gè)Animal基類,此基類有三個(gè)繼承類,分別是Cat、Dog、Tiger,基類與三個(gè)繼承類都有voice函數(shù)用于發(fā)聲,Animal發(fā)聲為~,Cat發(fā)聲為meow,Dog發(fā)聲為wang,Tiger發(fā)聲為howl。補(bǔ)全類定義,并寫一個(gè)函數(shù),要求傳入三個(gè)不同的動(dòng)物類的實(shí)例后會(huì)輸出不同的叫聲。有若干行輸入,每行是cat、dog、tiger之一,輸出是他們相對(duì)應(yīng)的叫聲.
謝謝!或者把您做出來的代碼貼出來參考一下
返回小木蟲查看更多
京公網(wǎng)安備 11010802022153號(hào)
你的程序不是已經(jīng)寫完了嗎?你還有什么問題?程序不能運(yùn)行的話,就貼出錯(cuò)誤信息
能運(yùn)行,我把題目意思搞錯(cuò)了,這個(gè)東西是對(duì)的!謝謝回復(fù),分都給你了
,