#include <iostream>
using namespace std;
class tricie
{
public:
tricie(int);
~tricie();
void acc();
void deacc();
private:
int df_speed=8;
};
tricie::tricie(int speed_input) //Shouldn't this make the speed=8?
{
int speed;
if (speed_input > df_speed)
{
speed=speed_input;
cout<<"speed now is "<<speed<<" km/hr \n";
}
else
{
speed=df_speed;
cout<<"speed now is "<<speed<<" km/hr \n";
}
}
tricie::~tricie()
{
}
void tricie::acc()
{
int speed=(speed+1);
cout<<"speed now is "<<speed<<" km/hr \n";
}
void tricie::deacc()
{
int speed=(speed-1);
cout<<"speed now is "<<speed<<" km/hr \n";
}
int main(int argc, const char * argv[]) //Xcode but these stuff in the bracket (ignore)
{
tricie biker1(4); //If i put any number > 8 the program work correctly
biker1.acc(); //output from this line is "speed now is 5 km/hr " instead of 9
biker1.deacc(); //output from this line is "speed now is 4 km/hr " instead of 8
biker1.deacc(); //output from this line is "speed now is 3 km/hr " instead of 7
return 0;
}
我想知道我是否遗漏了一个概念,它告诉我为什么输出 8 5 4 3 而不是 8 9 8 7?
提前感谢,如果问题很愚蠢,我很抱歉,我正在使用 sams 在 24 小时书中自学 C++。
感谢大家的快速回复我会搜索是否有办法防止在这些情况下编译提出我有一个后续问题:当我将 int speed 放在课堂的私人部分并且它工作正常但我想知道因为我把它放在类的私有部分并且主函数可以看到它,所以我没有得到另一个错误吗?