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

Write a class named \"Car\" that has the following member variables: In addition

ID: 3533875 • Letter: W

Question

Write a class named "Car" that has the following member variables:

In addition, the class should have the following member functions:

Demonstrate operation of the "Car" calss using a menu program with the following options:

Display the current speed after each selection and re-display the menu options use a function with prototype:

void display_menu();

Test your program by accelerating the car beyond 55 mph. Demonstrate that your class will not allow such reckless behavior (the max speed should display as 55 mpg). Similarly, decelerate down to zero demonstrating speed does not go negative.

Explanation / Answer

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
class Car
{
     private:
         int YearModel;
         string Make;
         int Speed;
     public:
         Car(int, string, int);
         string getMake() const;
         int getModel() const;
         int getSpeed() const;
         void Accelerate();
         void Brake();
};
Car::Car(int YearofModel, string Makeby, int Spd)
     : YearModel(YearofModel), Make(Makeby), Speed(Spd)
{
}
string Car::getMake() const
{
     return Make;
}
int Car::getModel() const
{
     return YearModel;
}
int Car::getSpeed() const
{
     return Speed;
}
void Car::Accelerate()
{
     Speed = Speed + 5;
}
void Car::Brake()
{
     Speed = Speed - 5;
}
void displayMenu()
{
     cout <<" Menu ";
     cout << "---------------------------- ";
     cout << "A)Accelerate the Car ";
     cout << "B)Push the Brake on the Car ";
     cout << "C)Exit the program ";
     cout << "Enter your choice: ";
}
int main()
{
     int Speed = 0; //Start Cars speed at zero.
     char choice; //Menu selection
     cout << "The speed of the car is set to: " << Speed <<endl;
     Car first( 2008, "Suzuki", Speed);
     //Display the menu and get a valid selection
     do
     {
         displayMenu();
         cin >> choice;
         while(toupper(choice) < 'A' || toupper(choice) > 'C')
         {
             cout << "Please make a choice of A or B or C:";
             cin >> choice;
         }
         switch (choice) {
             case 'a':
             case 'A': cout << "You are accelerating the car. ";
                 first.Accelerate();
                 break;
             case 'b':
             case 'B': cout << "You have choosen to push the brake. ";
                 first.Brake();
                 break;
         }
         cout << "Car speed is now " << first.getSpeed() << ' ';
     } while (toupper(choice) != 'C');
}

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