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;
}
.
|