求一道C++题目答案

2025-05-06 03:51:30
推荐回答(2个)
回答1:

#include 

using namespace std;

class bird

{

 private:

  int IsFly;

 public:

    bird(int=0);

    int Get();

};

int bird ::Get()

 cin>>IsFly;

 return IsFly;

}

bird::bird(int a) //构造函数 

{

 IsFly=a;

}

int main()

{

 bird a;

 int x;

 cout<<"请输入鸟的类型:\n";

 cout<<"\t0:一只会飞的鸟\n\t1:一只不会飞的鸟\n       -1:退出\n";

 while(1) 

 {

  x=a.Get();

  switch(x)

  {

   case  0 : cout<<"  它是一只会飞的鸟\n";break;

   case  1 : cout<<"  它是一只不会飞的鸟\n";break;

   case -1 : return 0;

   default : break;

  }

 }

}

回答2:

#include "stdio.h"

class bird
{
private:
int IsFly;
public:
bird(int n=0)
{
IsFly=n;
}
int Get()
{
return IsFly;
}
};

main()
{
int type=0;
printf("Input bird Type 0 or 1:");

scanf("%d",&type);

bird Thebird(type);

if(Thebird.Get()==0)
printf("The bird can not fly!\n");
else
printf("The bird can fly!\n");
}