Help with Car Class

Please help me Im having trouble with this Program!

I am to write a class named Car with the following members:
int year
string make
int speed
A constructor that accepts the cars year and make as arguments and assign those values to object's year and make variable. The constructor should initialize the speed member variable to 0.
Appropriate accessors functions should be created to allow values to be retrieved from an objects year, make, and speed member variables.
The accelerate function should add 5 to the speed member variable each time its called.
the brake function should subtract 5 from the speed member variable each time it is called.
Demostrate the class in a program that creates a Car object and then calls the accelerate function five times. After each call to the accelerate functions get the current speed of the car and display it. Then call brake function five times. After each call to the brake function get the current speedof the car and display it.

ERRORS:
: see declaration of 'Car'
error C2065: 'speed' : undeclared identifier
): error C2661: 'Car::Car' : no overloaded function takes 2 arguments
error C2039: 'accelerate' : is not a member of 'Car'
see declaration of 'Car'
: error C2143: syntax error : missing ',' before '<'
: error C2086: 'int i' : redefinition
see declaration of 'i'
syntax error : missing ';' before '{'
error C2039: 'Brake' : is not a member of 'Car'
see declaration of 'Car'
: fatal error C1075: end of file found before the left brace '{' at 'c:\users

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 "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Car
{
private:
int year;
int speed;
string make;
public:
Car(int, string, int);
int getYear()
{
	return year;
}
string getMake()
{
	return make;
}
int getSpeed()
{
	return speed;
}
};
Car::Car(int yr, string mk)
{
year=yr;
make=mk;
speed=0;
}
void Car::accelerate()
{
 speed +=5;
}
void Car::brake()
{
 speed -=5;
}

int main ()
{
	int yr;
	string mk;
	cout << "Please enter model year and make";
	cin >> yr,mk;

	Car myCar(yr,mk);
	for(int i=0; i < 5; i++)
	{
		myCar.accelerate();
		cout << "The Speed of my car is: " << myCar.getSpeed()<<endl;
		for(int i=0, i<5, i++)
		{
			myCar.Brake();
			cout << "The speed of my car is " << myCar.getSpeed()<<endl;
	}
		return 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
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
using namespace std;

class Car
{
private:
    int year;
    int speed;
    string make;
public:
    Car(int, string, int);
    int getYear() const /* const added */
    {
        return year;
    }
    string getMake() const /* const added */
    {
        return make;
    }
    int getSpeed() const /* const added */
    {
        return speed;
    }

    void accelerate() ; // *** added
    void brake() ; // *** added

};

//Car::Car(int yr, string mk)
Car::Car(int yr, string mk, int sp = 0 )
{
    year=yr;
    make=mk;
    //speed=0;
    speed = sp ;
}

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

int main ()
{
    int yr;
    string mk;
    cout << "Please enter model year and make";
    // cin >> yr,mk;
    cin >> yr >> mk ;

    Car myCar(yr,mk);
    for(int i=0; i < 5; i++)
    {
        myCar.accelerate();
        cout << "The Speed of my car is: " << myCar.getSpeed()<<endl;
        //for(int i=0, i<5, i++)
        for(int j=0 ; j<5 ; j++)
        {
            //myCar.Brake();
            myCar.brake();
            cout << "The speed of my car is " << myCar.getSpeed()<<endl;
        }
        // return 0; // *** removed
    }
} // *** added 
Topic archived. No new replies allowed.