当前位置: 首页 > 有奖问答 >C++的简单问题(我是菜鸟)

C++的简单问题(我是菜鸟)

作者 简单疯狂
来源: 小木虫 100 2 举报帖子
+关注

#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!=" &quot
        {
               
                if(a=="cat&quot
                {
                        Cat cat;
                        cat.sounds();
                }else if(a=="dog&quot
                {
                        Dog dog;
                        dog.sounds();
                }
                else if(a=="tiger&quot
                {
                        Tiger tiger;
                        tiger.sounds();
                }else if(a!="cat"||a!="dog"||a!="tiger&quot
                {
                        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之一,输出是他们相对应的叫声.
谢谢!或者把您做出来的代码贴出来参考一下 返回小木虫查看更多

今日热帖
  • 精华评论
  • 硫酸亚铜

    你的程序不是已经写完了吗?你还有什么问题?程序不能运行的话,就贴出错误信息

  • 简单疯狂

    引用回帖:
    2楼: Originally posted by 硫酸亚铜 at 2014-04-01 21:14:24
    你的程序不是已经写完了吗?你还有什么问题?程序不能运行的话,就贴出错误信息

    能运行,我把题目意思搞错了,这个东西是对的!谢谢回复,分都给你了

猜你喜欢
下载小木虫APP
与700万科研达人随时交流
  • 二维码
  • IOS
  • 安卓