Write a class named Car that has the following member variables: - year. An int
ID: 3538949 • 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 object that holds the make of the car.
- speed. An int that holds the cars current speed.
In addition, the class should have the following member functions.
- Constructor. The constructor should accept the car-s year and make as arguments
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 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 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.
Explanation / Answer
please rate - thanks
any questions just ask
#include <string>
#include <iostream>
using namespace std;
class Car
{
public:
Car(int,string);
void setSpeed(int);
void setYear(int);
void setMake(string);
int getSpeed();
string getMake();
int getYear();
void accelerate();
void brake();
private:
int year;
string make;
int speed;
};
Car::Car(int y,string m)
{year=y;
make=m;
speed=0;
}
void Car::setSpeed(int s)
{speed=s;
}
void Car::setMake(string m)
{ make=m;
}
void Car::setYear(int y)
{year=y;
}
int Car::getSpeed()
{return speed;
}
string Car::getMake()
{return make;
}
int Car::getYear()
{return year;
}
void Car::accelerate()
{speed+=5;
}
void Car::brake()
{speed-=5;
}
int main()
{Car a(2012,"Kia");
int i;
cout<<"For your "<<a.getYear()<<" "<<a.getMake()<<endl;
for(i=1;i<=5;i++)
{a.accelerate();
cout<<"After accelerating "<<i<<" time speed: "<<a.getSpeed()<<endl;
}
for(i=1;i<=5;i++)
{a.brake();
cout<<"After brakeing "<<i<<" time speed: "<<a.getSpeed()<<endl;
}system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.