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

In c++, write a class named Car that has the following member variables: yearMod

ID: 3823725 • Letter: I

Question

In c++, write a class named Car that has the following member variables:

yearModel A int that holds the cars year model

make A string that holds the make of the car

speed an int that holds the cars current speed.

In addition the calss should have the following constructor and other member functions.

constructor. The constructor should accept the cars year and model and make as arguments. These values should be assaigned to the objects yearModel and make member variables. The constructor should also be assign 0 to the speed member variables.

Accessor. appropirate accessor functions to get the values stored in an objects yearModel, make, and speed member variables

accelerate. The accelerate function shold 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.

Demostrate 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

Here is the code for the above scenario:

#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();

};

int Car::getSpeed()

{

return 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 >> yearModel >> make;

Car myCar(yearModel, 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);

}

}

}

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