-让我先介绍一些背景-我的任务是拍摄一个给定的情景(我的狗伙伴在后院看到一只青蛙,如果他饿了就吃它,如果不是他会玩它,如果他已经吃了两个他会放手吧。如果他看到一只猫或松鼠,他会向它吠叫,如果另一只狗他追它,如果一只土狼他会呼救,任何其他动物他都会看着它)。然后我们要让它计算给定晚上的动物数量,并将其与 Buddy 对所述动物的反应一起记录到另一个文件中。一个人将能够在记录的文件中输入一个日期,并为所述日期提取动物和互动。-
这是我目前拥有的代码:
#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;
}
编译时没有错误,但是当我对输出进行代码检查时,我得到一个错误,上面写着“第 100 行:警告:'number' is used uninitialized in this function”