C++ 如何在C++;?

C++ 如何在C++;?,c++,C++,-让我先介绍一些背景知识——我的任务是学习一个给定的塞纳里奥(我的狗伙伴在后院看到一只青蛙,如果他饿了,他会吃掉它,如果他不饿,他会玩它,如果他已经吃了两只,他会让它走。如果他看到一只猫或一只松鼠,他会对它吠叫,如果另一只狗追赶它,如果一只郊狼他会呼救,任何其他动物他都会看着它)然后,我们要让它计算某个晚上的动物数量,并将其与巴迪对所述动物的反应一起记录到另一个文件中。一个人要能够在记录的文件中输入日期,并为所述日期调出动物和互动- 以下是我目前拥有的代码: #include <iostr

-让我先介绍一些背景知识——我的任务是学习一个给定的塞纳里奥(我的狗伙伴在后院看到一只青蛙,如果他饿了,他会吃掉它,如果他不饿,他会玩它,如果他已经吃了两只,他会让它走。如果他看到一只猫或一只松鼠,他会对它吠叫,如果另一只狗追赶它,如果一只郊狼他会呼救,任何其他动物他都会看着它)然后,我们要让它计算某个晚上的动物数量,并将其与巴迪对所述动物的反应一起记录到另一个文件中。一个人要能够在记录的文件中输入日期,并为所述日期调出动物和互动-

以下是我目前拥有的代码:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class animal{
public:
   animal();
   ~animal();
   virtual string interactWithBuddy()//all derived classes use this
   {
        return "Buddy ";
   } 

};
animal::animal()
{
}
class frog: public animal
{

 public:
         string interactWithBuddy()
         {
              return "Buddy \n";
         }
         static int ID()
         {
             return 1;//ID assigned to frog for randomization purposes
         }         
};
class dog: public animal
{                
   public:
         string interactWithBuddy()
         {
              return "Buddy chased the dog\n";
         }
         static int ID()
         {
             return 2;//ID assigned to dog for randomization purposes
         }

};
class cat: public animal
{
  public:
         string interactWithBuddy()
         {
              return "Buddy barked at the cat \n";
         }
         static int ID()
         {
             return 3;//ID assigned to cat for randomization purposes
         }
};
class coyote: public animal
{
  public:
         string interactWithBuddy()
         {
              return "Buddy cried for help when he seen the coyote \n";
         }
         static int ID()
         {
             return 4;//ID assigned to coyote for randomization purposes
         }
};
class squirrel: public animal
{
  public:
         string interactWithBuddy()
         {
                return "Buddy barked at the squirrel \n";
         } 
         static int ID()
         {
             return 5;//ID assigned to squirrel for randomization purposes
         }
};
class otherAnimal: public animal
{
  public:
         string interactWithBuddy()
         {
                return "Buddy watched the animal \n";
         } 
         static int ID()
         {
             return 6; //ID assigned to otherAnimal for randomization purposes
         }
};
int main ()
{
srand(time(0)); //intializes the random seed
int number;
animal * a; // pointer to animal 
  std::cout << (rand() % 6 + 1) <<std::endl;  //return random number between 1-6

 // loop to assign the random number output a proper animal ID
  if (number == frog::ID()) 
  {
        a = new frog;
        a->interactWithBuddy();

  }
  else if (number == dog::ID())
  {
       a = new dog;
       a->interactWithBuddy();
  }
  else if (number == cat::ID())
  {
       a = new cat;
       a->interactWithBuddy();
  }
  else if (number == coyote::ID())
  {
       a = new coyote;
       a->interactWithBuddy();
  }
  else if (number == squirrel::ID())
  {
     a = new squirrel;
     a->interactWithBuddy();
  }
  else if (number == otherAnimal::ID()) 
  {
      a = new otherAnimal;
      a->interactWithBuddy();
  }

return 0;
}
#包括
#包括
#包括
使用名称空间std;
类动物{
公众:
动物();
~animal();
虚拟字符串InteractiveWithBuddy()//所有派生类都使用此
{
返回“好友”;
} 
};
动物
{
}
青蛙类:公共动物
{
公众:
字符串interactivewithbuddy()
{
返回“Buddy\n”;
}
静态int-ID()
{
返回1;//为随机化目的分配给frog的ID
}         
};
犬类:公共动物
{                
公众:
字符串interactivewithbuddy()
{
return“Buddy追赶狗\n”;
}
静态int-ID()
{
返回2;//为随机化目的分配给狗的ID
}
};
猫类:公共动物
{
公众:
字符串interactivewithbuddy()
{
return“Buddy对猫吠叫\n”;
}
静态int-ID()
{
返回3;//为随机化目的分配给cat的ID
}
};
郊狼类:公共动物
{
公众:
字符串interactivewithbuddy()
{
return“Buddy看到郊狼时哭着求救\n”;
}
静态int-ID()
{
返回4;//为随机化目的分配给郊狼的ID
}
};
松鼠类:公共动物
{
公众:
字符串interactivewithbuddy()
{
return“Buddy对着松鼠吠叫\n”;
} 
静态int-ID()
{
返回5;//为了随机化目的分配给squirrel的ID
}
};
其他动物类:公共动物
{
公众:
字符串interactivewithbuddy()
{
return“Buddy观看了动物\n”;
} 
静态int-ID()
{
返回6;//为随机化目的分配给其他动物的ID
}
};
int main()
{
srand(time(0));//初始化随机种子
整数;
animal*a;//指向animal的指针
std::不能与buddy()交互;
}
else if(number==coyote::ID())
{
a=新的郊狼;
a->interactivewithbuddy();
}
else if(number==squirrel::ID())
{
a=新松鼠;
a->interactivewithbuddy();
}
else if(number==otherAnimal::ID())
{
a=新的其他动物;
a->interactivewithbuddy();
}
返回0;
}
编译时没有错误,但当我对输出进行代码检查时,会出现一个错误,显示“第100行:警告:此函数中未初始化使用了'number'”

int
意味着函数返回一个整数。但它实际上返回一个字符串(
const char*
)。编译器不知道如何将返回的
const char*
转换为应该返回的
int


int
表示函数返回一个整数。但实际上它返回一个字符串(
const char*
)。编译器不知道如何将返回的
const char*
转换为应该返回的
int

至于第二个问题,您正在从函数返回一个cstring,但您已将函数的返回类型声明为整数。双引号之间的任何内容“”被视为常量字符串。请更改函数的返回类型或返回整数。对于第二个问题,您正在从函数返回cstring,但已将函数的返回类型声明为整数。双引号之间的任何内容“”被视为常量字符串。请更改函数的返回类型或返回一个整数

您的函数被声明为返回一个整数-您返回的是以null结尾的字符串

相反,在基类中,可能会返回一个错误类型ID,如

 virtual int ID()
 {
     return -1;
 }

您的函数被声明为返回一个整数-您返回的是以null结尾的字符串

相反,在基类中,可能会返回一个错误类型ID,如

 virtual int ID()
 {
     return -1;
 }

对于您的第一个问题,这可能需要几次尝试

首先,假设您只是想随机选择一种动物:

srand(time(0)); //initializes the random seed
int number = rand() % 6 + 1;
animal *a;
if(number == 1)
{
  a = new frog;
}
if(number == 2)
{
  a = new dog;
}
...

a->interactWithBuddy();
delete(a); // Don't forget to delete what you create with "new".
这是可行的,但ID号在这里是“硬编码”的,它不使用您编写的
ID()
函数。如果您想使用
ID()
,您可以让每只动物各有一个,然后查看哪一个与
number
匹配:

frog Kermit;
dog Ralph;
cat Felix;
coyote Loki;
squirrel Sparticus;

if(number == kermit.ID())
{
  kermit.interactWithBuddy();
}
if(number == Ralph.ID())
{
  Ralph.interactWithBuddy();
}
...
你必须事先准备好每只动物,因为在动物存在之前,你不能向动物索要
ID()
。但是有一种方法可以对
ID()
进行编码,这样你就可以在得到某一种动物之前,通过使用“
static
”来索要该动物的ID:

这也解决了您的第二个问题,因为在
animal
中不再有有问题的
OD()

这足够了吗?还有其他的可能性

编辑:


您忘记了
int number=rand()%6+1;

对于第一个问题,这可能需要尝试几次

首先,假设您只是想随机选择一种动物:

srand(time(0)); //initializes the random seed
int number = rand() % 6 + 1;
animal *a;
if(number == 1)
{
  a = new frog;
}
if(number == 2)
{
  a = new dog;
}
...

a->interactWithBuddy();
delete(a); // Don't forget to delete what you create with "new".
这是可行的,但是ID号在这里是“硬编码”的,它不使用您编写的
ID()
函数。如果您想使用
ID()
,您可以让每只动物都有一个,看看哪一个匹配