help with classes.

I'm not sure what I am doing wrong, I keep getting error syntax with the "{" in "{ return current_speed; }" , but I get more errors if I take it off.

Instruction:

Write a class named Car that has the following member variables:
yearModel, make, speed. In addition the class should have the following constructor and the other member functions
constructor-accept the cars year model make as arguments. an assign to the objects year, make also assign speed 0 to speed member.
Accessors to get the values stored in an object's yearModel make, speed
accelerate- should add 5 to the speed member variable each time it is called.
brake- subtract 5 from speed each time it is called.
Demonstrate the class in a program that creates a Car object and then calls the accelerate function five times. After each call to the accelerate function get the current speed of the car and display it. Then call the brake function five times after each call to the brake function, get the current speed. of the car and display it.

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
#include <iostream>
#include <string>

using namespace std;
class Car
{
private:
int yearModel;
string make;
int speed;

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

{ return current_speed; }
};

Car::Car(int yearModel, string make, int speed = 0 )

{
void Car::accelerate()
{
speed +=5;
}
void Car::brake()
{
if( speed > 5 )
speed -=5;
else speed = 0 ;
}


int main ()
{
int yearModel;
string make;
cout << "Enter year and make ";

cin >> year >> make ;

Car myCar(year,make);
for(int i=0; i < 5; i++)
{
myCar.accelerate();
cout << "The speed of the car is: " << myCar.getSpeed()<<endl;

for(int j=0 ; j<5 ; j++)
{

myCar.brake();
cout << "The speed of the car is " << myCar.getSpeed()<<endl;

system ("PAUSE");
return (0)
}
I think you meant to define "getSpeed"

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
62
63
64
65
66
67
68
#include <iostream>
#include <string>

using namespace std;
class Car
{
private:
int yearModel;
string make;
int speed;

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


};

int Car::getSpeed()
{
    return current_speed;
    return speed;
}

Car::Car(int yearModel, string make, int speed = 0 )
{
    //you had no definition here, and you were missing '{' and '}'
}

{
void Car::accelerate()
{
speed +=5;
}
void Car::brake()
{
if( speed > 5 )
speed -=5;
else speed = 0 ;
}


int main ()
{
int yearModel;
string make;
cout << "Enter year and make ";

cin >> yearModel >> make ;

Car myCar(yearModel,make);
for(int i=0; i < 5; i++)
{
myCar.accelerate();
cout << "The speed of the car is: " << myCar.getSpeed()<<endl;

for(int j=0 ; j<5 ; j++)
{

myCar.brake();
cout << "The speed of the car is " << myCar.getSpeed()<<endl;

system ("PAUSE");
return (0)
}


You have no variable defined as "current_speed", so your compiler can't compile it. Change it to "speed" so that it matches with your class definition.
thank you i made the correction.
I'm getting an error saying "left of '.getSpeed' must have class/strut/union" and i thought myCar was the class.
This is the code that I have that works with my compiler.
I believe that there were some missing braces in the code I posted above.


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
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>

using namespace std;
class Car
{
private:
	int yearModel;
	string make;
	int speed;

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


};

int Car::getSpeed()
{
	
		return speed;
}

Car::Car(int yearModel, string make, int speed = 0)

{

}

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

void Car::brake()
{
	if (speed > 5)
		speed -= 5;
	else speed = 0;
}


int main()
{
	int yearModel;
	string make;
	cout << "Enter year and make ";

	cin >> yearModel >> make;

	Car myCar(yearModel, make);
	for (int i = 0; i < 5; i++)
	{
		myCar.accelerate();
		cout << "The speed of the car is: " << myCar.getSpeed() << endl;

		for (int j = 0; j < 5; j++)
		{

			myCar.brake();
			cout << "The speed of the car is " << myCar.getSpeed() << endl;

			system("PAUSE");
			return (0);
		}
	}
}


P.S: Do you want your constructor to do anything? If so, you might want to fill in the definition.
I do I just don't know how. I've been messing with the code for quite some time now and tried reading some forums but I'm still not getting it right.

And thank you I didn't realize i need more braces.
Well, if you post here what issues you are having, the lovely people here on the forum are always more than happy to help.

What about your code isn't working the way you want it to?
The code is working i'm just trying to figure out why my acceleration is a huge negative number when I input the year and make of the car.

EDIT: I finally fixed the negative number on there. Thanks for all your help.
Last edited on
Topic archived. No new replies allowed.