Your accelerate() method takes a parameter called speed. But the class also has a member called speed. So if you use "speed" inside the method, which one do you mean? The class member? The parameter? This is like throwing marbles on the floor: it's guaranteed to trip you up. By the way, the compile will use the parameter, not the class member.
For this reason, it's a good idea to never use a class member name as a parameter of the class's method. Let's change the parameter to in_speed:
1 2 3 4 5 6 7 8
|
int Car::accelerate(int in_speed)
{
if (in_speed < 80)
{
setSpeed(in_speed + 10);
return 0;
}
}
|
The same logic applies to brake(). You
Hmm... This doesn't make sense does it? The comments say that it should just increase your speed by 10. It looks like your code meant the member variable "speed" instead of the parameter. Let's fix that:
1 2 3 4 5 6 7 8
|
int Car::accelerate(int in_speed)
{
if (speed < 80)
{
setSpeed(speed + 10);
return 0;
}
}
|
Notice that the parameter in_speed is never used now. So why do you need it?
The same problem is in brake(). The code is using the parameter speed, not the member variable.
Now let's look at setSpeed:
1 2 3 4
|
void Car::setSpeed(int)
{
speed = speed;
}
|
You haven't named the parameter, which means it won't be used. That should make a big loud bell go off in your head. How can you set the speed when you haven't even named the parameter that contains the value you're setting it to??
The statement
speed = speed;
takes the member variable speed and assigns it to itself. That's a big "do nothing" statement and the compiler will optimize it out of your code. You should be able to fix this based on the other comments above on accelerate and brake