Class Object, Display Speed of Vehicle

Prompt written below. Current problem; all displayed current speeds are negative and incorrect.

Prompt: For this program I need to demonstrate class in a program that creates a Car object. I need the program to call an accelerate function 5 times, after each acceleration iteration of +5 mph I need it to display the current speed of the vehicle. I then need the program to call a brake function 5 times, after each deceleration iteration of -5 mph I need it to display the current speed of the vehicle. The speeds displayed should look something like this

The current speed of the vehicle is 5
... 10
... 15
... 20
... 25
... 20
... 15
... 10
... 5
... 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include<string>
using namespace std;

class Car
{
private:
	double YearModel;
	string Make;
	double Speed;

public:
	Car(double, string, double);
	double getSpeed();
	double getModel();
	void accelerate();
	void brake();
};

double Car::getSpeed()
{
	return Speed;
}

Car::Car(double YearModel, string Make, double Speed = 0)
{

}

void Car::accelerate()
{
	Speed = Speed + 5;
}

void Car::brake()
{
	Speed = Speed - 5;
}

int main()
{
	double YearModel;
	string Make;
	cout << "Enter the vehicles year and make ";
	cin >> YearModel >> Make;

	Car myCar(YearModel, Make);
	for (int a = 0; a < 5; a++)
	{
		myCar.accelerate();
		cout << "The current speed of the vehicle is: " << myCar.getSpeed() << endl;
	}

	for (int b = 0; b < 5; b++)
	{
		myCar.brake();
		cout << "The current speed of the vehicle is: " << myCar.getSpeed() << endl;
	}
	system("Pause");
	return 0;
}
You never set Speed to a starting value, so Speed is currently equal to garbage.

1
2
3
4
Car::Car(double YearModel, string Make, double Speed = 0)
{

}


Car myCar(YearModel, Make);

You call the constructor here, but you're missing the speed part. Your constructor takes a double, string and then double, you only give it a double and a string.
Last edited on
I'm sorry, this is flying a little over my head. Are you saying I should set the variable Speed? I though I did so by

 
double Speed=0
You're doing that inside The constructor's parameters. It's not exactly what you think it is. That constructor is made so that you will send in 3 values to it, and it will give those values to your private variables.

Car::Car(double YearModel, string Make, double Speed = 0)

This constructor takes 3 values as you can see for yourself. Your problem is that you're only sending in to 2 out of 3 Car myCar(YearModel, Make); you're missing the speed part.

I believe you need to ask the user for the speed. And then send it in so it looks like this

Car myCar(YearModel, Make, speed);

And inside the constructor initialize your private variables.

1
2
3
4
5
6
7
Car::Car(double userYearModel, string userMake, double userSpeed)
{
	Speed = userSpeed;
	YearModel = userYearModel;
	Make = userMake;
	
}


Now, all the values the user entered for the model, make and speed, is stores in the variables you created for your class

1
2
3
4
5
6
class Car
{
private:
	double YearModel;
	string Make;
	double Speed;


edit: I changed the name of the variables inside the constructor parameter so you can see things more clearly, it can be confusing when they have the same name.
Last edited on
Got it! Thankyou for the help.
Topic archived. No new replies allowed.