#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 を配置し、正常に動作していますが、知りたいですクラスのプライベート部分に入れ、メイン関数がそれを見ることができるので、私が取得していない別のエラーはありますか?