Write a class named Car that has the following member variables: year : An int tht holds the cars model year. make : A string object 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 member functions. Constructor: The constructor should accept the car's year and make as assignments and assign these values to object's year and make member variables. The contructor should initialize the speed member variable to 0. Accessors: Appropriate accessor functions should be created to allow values to be retrieved from an object's year, make, and speed member variables. accelerate: The accelerate function should add 5 to the speed member variable each time it is called. brake: The brake function should subtract 5 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 display it.

Respuesta :

Answer:

#include <iostream>

#include <cstring>

#include <cctype>

#include <conio.h>

using namespace std;

class Car

{

private:

int YearModel;

int Speed;

string Make;

public:

Car(int, string);

Car();

string getMake();

int getModel();

int getSpeed();

void Accelerate();

void Brake();

};

Car::Car()

{cout<<"Enter car year: ";

cin>>YearModel;

cout<<"Enter car make: ";

cin>>Make;

Speed=0;

}

Car::Car(int YearofModel, string Makeby)

{

YearModel = YearofModel;

Make = Makeby;

Speed = Spd;

}

//To get who makes the car.

string Car::getMake()

{

return Make;

}

//To get the year of the car.

int Car::getModel()

{

return YearModel;

}

//To holds the car actual speed.

int Car::getSpeed()

{

return Speed;

}

//To increase speed by 5.

void Car::Accelerate()

{

Speed = Speed +5;

cout<<"The "<<YearModel<<" "<<Make<<" is accelerating. The speed is now "<<Speed<<" mph."<<endl;

}

//To drop the speed of the car by 5.

void Car::Brake()

{

Speed = Speed -5;

cout<<"The "<<YearModel<<" "<<Make<<" is braking. The speed is now "<<Speed<<" mph."<<endl;

}

int main()

{

int Speed = 0; //Start Cars speed at zero.

int index;

int total=0;

Car mine;

mine.Accelerate();

mine.Brake();

cout<<"Your Caprice's speed is: "<<Speed<<endl;

Car first( 1990, "Chevy", Speed);

total+=Speed;

//Display the menu and get a valid selection

for(index=0;index<6;index++)

{

first.Accelerate();

// here u have to change Speed to //first.getSpeed()

}

for(index=0;index<6;index++)

{

first.Brake();

// here u have to change Speed to //first.getSpeed()

}

_getch();

return 0;

}