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)
}
|