C++ 成员函数中的变量生存期,值不断重置为“0”,为什么?
问题描述 我正试图为孩子们编写一个甲虫游戏的实现。每当我调用函数时,下面的代码将C++ 成员函数中的变量生存期,值不断重置为“0”,为什么?,c++,class,member-functions,C++,Class,Member Functions,问题描述 我正试图为孩子们编写一个甲虫游戏的实现。每当我调用函数时,下面的代码将翅膀,腿,头部等初始化为零 初始化应该在哪里,以便我可以在第二次调用函数时增加翅膀、腿、头部等,而不会丢失它们的值 #包括 #包括 #包括 使用名称空间std; 级模 { 私人: int-num; 公众: int roll(); int getNum(); 无效过程(); }; int Die::roll()//掷一个骰子得到随机数 { srand((未签名)时间(0)); num=(rand()%6)+1; co
翅膀
,腿
,头部
等初始化为零
初始化应该在哪里,以便我可以在第二次调用函数时增加翅膀
、腿
、头部
等,而不会丢失它们的值
#包括
#包括
#包括
使用名称空间std;
级模
{
私人:
int-num;
公众:
int roll();
int getNum();
无效过程();
};
int Die::roll()//掷一个骰子得到随机数
{
srand((未签名)时间(0));
num=(rand()%6)+1;
cout当前int-eyes=0,antens=0,legs=0,wings=0,head=0,body=0;
是Die::process
中的局部变量,这意味着它们被绑定到声明它们的范围
每次执行函数时,它们都将被初始化为0
,并且无法在函数调用之间保持当前编写的状态
如果希望将变量绑定到Die
本身的实例,则应将其添加为数据成员(1),如下面的示例所示
这样,在调用Die
中的成员函数之间,值将保持不变
class Die
{
private:
int num;
int eyes, antennas, legs, wings, head, body; // (1)
public:
int roll();
int getNum();
void process();
Die () : eyes (0), antennas (0), legs (0), wings (0), head (0), body (0) { }; (2)
};
在上面,我们使用类Die
的构造函数在创建Die
实例时将它们全部初始化为0
(2),并从void Die::Process()中删除相关变量名的声明
您将变量声明为函数的本地变量。这就是为什么每次调用函数时都会重置这些变量。要保留函数调用之间的值,必须将变量移到类的成员中
你的Die
类也没有很好的设计。你应该只播种生成器一次,所以在构造函数中这样做,然后创建Die
对象一次,而不是每次循环迭代。而且你没有在新的roll中使用最后一个roll,因此类中不需要有num
成员
您还应该将甲虫逻辑与Die
类分开。骰子不能控制游戏。游戏使用骰子来做出决策。您的代码应该反映这一点
请尝试类似以下内容:
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
class Dice
{
public:
Dice();
int roll();
};
class Beetle
{
private:
int eyes;
int antennas;
int legs;
int wings;
int head;
int body;
public:
Beetle();
void takeATurn(Dice &dice);
};
Beetle::Beetle() :
eyes(0),
antennas(0),
legs(0),
wings(0),
head(0),
body(0)
{
}
Dice::Dice() // initialize the dice
{
srand((unsigned)time(0));
}
int Dice::roll() //roll one dice to get random number
{
int num = (rand() % 6)+1;
cout << num;
return num;
}
void Beetle::takeATurn(Dice &dice) //player is building the beetle
{
int num = dice.roll();
if ((num != 6) && (body < 1))
{
cout << endl << "You need a body first";
return;
}
switch (num)
{
case 1:
if (eyes < 2)
{
eyes++;
cout << endl << "You got an eye";
}
else
cout << endl << "Sorry, the beetle has only 2 eyes!";
break;
case 2:
if (antennas < 2)
{
antennas++;
cout << endl << "You got an antenna";
}
else
cout << endl << "No more than 2 antennas";
break;
case 3:
if (legs < 6)
{
legs++;
cout << endl << "You got a leg";
}
else
cout << endl << "Opps, you can't have more legs!";
break;
case 4:
if (wings < 2)
{
wings++;
cout << endl << "You got a wing";
}
else
cout << endl << "nope, the beetle got 2 wings only";
break;
case 5:
if (head < 1)
{
head++;
cout << endl << "You got the head";
}
else
cout << endl << "One head is enough";
break;
case 6:
if (body < 1)
{
body++;
cout << endl << "You got the body";
}
else
cout << endl << "You got the body already";
break;
}
}
}
int main()
{
int n = 0;
cout << "start?";
cin >> n;
if (n == 1)
{
Dice dice1;
Beetle beetle;
do
{
beetle.takeATurn(dice1);
cout << endl << "Repeat?: ";
cin >> n;
}
while (n == 1);
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类骰子
{
公众:
骰子();
int roll();
};
甲虫纲
{
私人:
内视眼;
智能天线;
内特腿;
内翼;
int头;
内体;
公众:
甲虫();
虚空(骰子和骰子);
};
甲虫::甲虫():
眼睛(0),
天线(0),
腿(0),
翅膀(0),
总目(0),,
正文(0)
{
}
骰子::骰子()//初始化骰子
{
srand((未签名)时间(0));
}
int Dice::roll()//掷一个骰子得到随机数
{
int num=(rand()%6)+1;
“甲虫游戏”是什么?
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
class Dice
{
public:
Dice();
int roll();
};
class Beetle
{
private:
int eyes;
int antennas;
int legs;
int wings;
int head;
int body;
public:
Beetle();
void takeATurn(Dice &dice);
};
Beetle::Beetle() :
eyes(0),
antennas(0),
legs(0),
wings(0),
head(0),
body(0)
{
}
Dice::Dice() // initialize the dice
{
srand((unsigned)time(0));
}
int Dice::roll() //roll one dice to get random number
{
int num = (rand() % 6)+1;
cout << num;
return num;
}
void Beetle::takeATurn(Dice &dice) //player is building the beetle
{
int num = dice.roll();
if ((num != 6) && (body < 1))
{
cout << endl << "You need a body first";
return;
}
switch (num)
{
case 1:
if (eyes < 2)
{
eyes++;
cout << endl << "You got an eye";
}
else
cout << endl << "Sorry, the beetle has only 2 eyes!";
break;
case 2:
if (antennas < 2)
{
antennas++;
cout << endl << "You got an antenna";
}
else
cout << endl << "No more than 2 antennas";
break;
case 3:
if (legs < 6)
{
legs++;
cout << endl << "You got a leg";
}
else
cout << endl << "Opps, you can't have more legs!";
break;
case 4:
if (wings < 2)
{
wings++;
cout << endl << "You got a wing";
}
else
cout << endl << "nope, the beetle got 2 wings only";
break;
case 5:
if (head < 1)
{
head++;
cout << endl << "You got the head";
}
else
cout << endl << "One head is enough";
break;
case 6:
if (body < 1)
{
body++;
cout << endl << "You got the body";
}
else
cout << endl << "You got the body already";
break;
}
}
}
int main()
{
int n = 0;
cout << "start?";
cin >> n;
if (n == 1)
{
Dice dice1;
Beetle beetle;
do
{
beetle.takeATurn(dice1);
cout << endl << "Repeat?: ";
cin >> n;
}
while (n == 1);
}
return 0;
}