C++ Car Class Project

I have written my code for a challenge I found in a text book however I am having trouble getting it to compile.
The assignment is as follows:
Write a class named Car that has the following member variables:
• yearModel. An int that holds the car’s year model.
• make. A string that holds the make of the car.
• speed. An int that holds the car’s current speed.
In addition, the class should have the following constructor and other member functions.
• Constructor. - This constructor should accept the car’s year model and make as arguments. These
values should be assigned to the object’s yearModel and make member variables. The constructor
should also assign 0 to the speed member variables.
• Observer (aka. Accessor). - Appropriate observer functions to get the values stored in an object’s
yearModel, make, and speed member variables.
• accelerate. – The accelerate function should add 10 to the speed member variable each time it is
called.
• brake. – The brake function should subtract 10 from the speed member variable 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
60
61
62
63
64
65
66
 #include <iostream>
#include <string>
using namespace std;

// car class declaration
class car
{
private:
    int year;
    string make;
    int speed;

public:
    // constructor with default parameters
     car(int y = 2010, string m = "unknown")
     {
         year = y;
             make  = m;
             speed = 0;
     }

     // Accessors ( The get functions)

     int getYear()
     { return year; }

     string getMake()
     { return make; }

     int getSpeed()
     { return speed; }

     // Mutators
     void accelerate {}
    {  speed += 10;  }

    void brake{}
    {

        if ( speed > = 10)
            speed - =10;
        else
            speed = 0;
    }

};

//*************MAIN*****************

int main()
{
     car hotRod (2010, "Mazda");
     cout << "I'm in my" << hot.Rod.getYear()<<" "
          << hotRod.getMake() << " hot rod.\n\n";
    // Now stop
    cout << "Now I'm breaking...\n\n";
    for (int slower =1; slower <= 10; slwoer++)
    (
     hotRod.brake();
     cout << " current speed: " << hotRod.getSpeed() << "mph";
     )
     cout << endl;
     return 0;
}

.
A few things, could be more.

line 34 () instead of {}
line 37 () instead of {}
line 40 >= instead of > =
line 41 -= intead fo - =
line 53 hotRod.getYear() instead of hot.Rod.getYear()
line 57 slower++ instead of slwoer++
line 58 { instead of (
line 61 } instead of )
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
#include <iostream>
#include <string>
using namespace std;
 
// car class declaration
class car
{
private:
    int year;
    string make;
    int speed;
 
public:
    // constructor with default parameters
     car(int y = 2010, string m = "unknown")
     {
         year = y;
             make  = m;
             speed = 0;
     }
 
     // Accessors ( The get functions)
 
     int getYear()
     { return year; }
 
     string getMake()
     { return make; }
 
     int getSpeed()
     { return speed; }
 
     // Mutators
     void accelerate ()
    {  speed += 10;  }
 
    void brake()
    {
 
        if ( speed >= 10)
            speed -=10;
        else
            speed = 0;
    }
 
};
 
//*************MAIN*****************
 
int main()
{
     car hotRod (2007, "Ford");
     cout << " I'm in my " << hotRod.getYear()<<" "
          << hotRod.getMake() << " hot rod.\n\n";
    // Now stop
    cout << "Now I'm breaking...\n\n";
    for (int slower =1; slower <= 10; slower++)
    {
 
 
     hotRod.brake();
     cout << " current speed: " << hotRod.getSpeed() << "mph";
    }
     cout << endl;
     return 0;
}


The code now compiles, however when it does every speed is set at 0mph any suggestions as to how to fix it?
Its because you never accelerate :P
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
74
75
76
77
78

#include <iostream>
#include <string>
using namespace std;
 
// car class declaration
class car
{
private:
    int year;
    string make;
    int speed;
 
public:
    // constructor with default parameters
     car(int y = 2010, string m = "unknown")
     {
         year = y;
             make  = m;
             speed = 0;
     }
 
     // Accessors ( The get functions)
 
     int getYear()
     { return year; }
 
     string getMake()
     { return make; }
 
     int getSpeed()
     { return speed; }
 
     // Mutators
     void accelerate ()
    {  speed += 10;  }
 
    void brake()
    {
 
        if ( speed >= 10)
            speed -=10;
        else
            speed = 0;
    }
 
};

int main()
{
     car hotRod (2007, "Ford");
     cout << " I'm in my " << hotRod.getYear()<<" "
          << hotRod.getMake() << " hot rod.\n\n";
    // Now stop
    cout << "Running...\n\n";
    for (int run =0; run < 5; run++)
    {
 
     hotRod.accelerate();
     cout << " current speed: " << hotRod.getSpeed() << "mph.\n";
    }
     cout << endl;
     
     // now breaking
     cout << "Now breaking... " << endl;
     for(int brk = 0; brk < 5; brk++)
     {
         hotRod.brake();
         cout << "  Current speed: " << hotRod.getSpeed() << "mph.\n";
         if(hotRod.getSpeed() == 0)
         {
             cout <<endl << hotRod.getMake() << "(" <<hotRod.getYear() <<")"<< " has now stopped\n";
         }
     }
     
     return 0;
}
I'm in my 2007 Ford hot rod.

Running...

 current speed: 10mph.
 current speed: 20mph.
 current speed: 30mph.
 current speed: 40mph.
 current speed: 50mph.

Now breaking... 
  Current speed: 40mph.
  Current speed: 30mph.
  Current speed: 20mph.
  Current speed: 10mph.
  Current speed: 0mph.

Ford(2007) has now stopped
Last edited on
Topic archived. No new replies allowed.