Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the question: Write a class named Car that has the following member vari

ID: 3621975 • Letter: T

Question

This is the question:
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 fuction, get the current speed. of the car and display it.

My code will compile, however, the speed stays at zero. Here is my code so far:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

class Car
{
private:
int YearModel;
int Speed;
string Make;
public:
Car(int, string, int);
string getMake();
int getModel();
int getSpeed();
void Accelerate();
void Brake();
};

Car::Car(int YearofModel, string Makeby, int Spd)
{
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;
}
//To drop the speed of the car by 5.
void Car::Brake()
{
Speed = Speed -5;
}



int main()
{
int Speed = 0; //Start Cars speed at zero.
int index;
int total=0;
cout<<"Your G-6 is speed is: "<<Speed<<endl;
Car first( 2008, "Pontiac", Speed);
total+=Speed;
//Display the menu and get a valid selection

for(index=0;index<6;index++)
{
first.Accelerate();
cout<<"The car is accelerating at "<<Speed<<" mph."<<endl;
}
cout<<"The car's speed current speed is "<<Speed<<" mph."<<endl;
for(index=0;index<6;index++)
{
first.Brake();
cout<<"The car is breaking. The speed is now "<<Speed<<" mph."<<endl;
}
cout<<"The car's speed is now "<<Speed<<" mph."<<endl;
return 0;
}

Explanation / Answer

// buddy u r 100% right but u r not supposed to call spped all the time

// the edited code given below according to your requirements.

// only two changes u have to make

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

class Car
{
private:
int YearModel;
int Speed;
string Make;
public:
Car(int, string, int);
string getMake();
int getModel();
int getSpeed();
void Accelerate();
void Brake();
};

Car::Car(int YearofModel, string Makeby, int Spd)
{
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;
}
//To drop the speed of the car by 5.
void Car::Brake()
{
Speed = Speed -5;
}



int main()
{
int Speed = 0; //Start Cars speed at zero.
int index;
int total=0;
cout<<"Your G-6 is speed is: "<<Speed<<endl;
Car first( 2008, "Pontiac", Speed);
total+=Speed;
//Display the menu and get a valid selection

for(index=0;index<6;index++)
{
first.Accelerate();
cout<<"The car is accelerating at "<<first.getSpeed()<<" mph."<<endl;

// here u have to change Speed to //first.getSpeed()                                                                                                         
}
cout<<"The car's speed current speed is "<<first.getSpeed()<<" mph."<<endl;
for(index=0;index<6;index++)
{
first.Brake();
cout<<"The car is breaking. The speed is now "<<first.getSpeed()<<" mph."<<endl;

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

}
cout<<"The car's speed is now "<<first.getSpeed()<<" mph."<<endl;
return 0;
}

// now compile and run CHeers.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote