Write a class named Car that has the following member variables: . year . An int
ID: 3622440 • Letter: W
Question
Write a class named Car that has the following member variables:
. year. An int that holds the car's model year.
.make. A string that holds the make of the car
.speed.An int that holds the car's current speed.
In addition, the class have the following member functions.
.constructor. The constructor should accept the car's year and make as argument and assign these values to the object's year and make member variables. The constructor should initialize the speed member variable to 0.
.Accessors. Appropriate accessor functions should be created to allow variables to be retrieve from an object's year, make, and speed member variables(e.g, getYear(),getMake(),getSpeed().
.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 create a Car object, and then calls the accelerate function five times.After each call to the accerelerate 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.
Sample Run:
Acceleration1. Current speed: 5 mph.
Acceleration2. Current speed: 10 mph.
Acceleration3. Current speed: 15 mph.
Acceleration4. Current speed: 20 mph.
Acceleration5. Current speed: 25 mph.
Deceleration1. Current speed: 20 mph.
Deceleration2. Current speed: 15 mph.
Deceleration3. Current speed: 10 mph.
Deceleration4. Current speed: 5 mph.
Deceleration5. Current speed: 0 mph.
Press any key to continue . . .
Explanation / Answer
please rate
#include
using namespace std;
class car{
int year;
char *make;
int speed;
public:
car(int,char[]);
int getSpeed();
int getYear();
void brake();
char* getMake();
void accelerate();
};
car::car ( int yr, char *mk ){
make = mk;
year = yr;
speed = 0;
}
void car::accelerate( ){
speed+=5;
}
void car::brake ( ){
speed-=5;
}
int car::getYear ( ){
return year;
}
int car::getSpeed ( ){
return speed;
}
int main(){
int i;
car c(1990,"abc");
for ( i=0;i<5;i++ ){
c.accelerate();
cout<<"Acceleration "< }
cout<<" ";
for ( i=0;i<5;i++ ){
c.brake();
cout<<"Deceleration "< }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.