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

C++ CODE Write a class named Car that has the following data members: • yearMode

ID: 3851108 • Letter: C

Question

C++ CODE

Write a class named Car that has the following data members: • yearModel: an int that holds the car’s year model • make: a string that holds the make of the car • speed: an int that holds the car’s current speed In addition, the class should have the following functions: • a constructor: The constructor accepts the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make data members. The constructor should also assign 0 to the data member speed. • accelerate: This function adds 10 to the speed member variable each time it is called. • brake: This function subtracts 5 from the speed member variable each time it is called. • accessor: Appropriate accessor functions to get the values stored in an object’s yearModel, make, and speed member variables. 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. Divide your program into Car.h, Car.cpp, and Lab5.cpp. (Do not forget to use include guards, #ifndef, #define, #endif). Sample Run: I am driving 2009 BMW. Current speed: 0 Accelerating... Current speed: 10 Accelerating... Current speed: 20 Accelerating... Current speed: 30 Accelerating... Current speed: 40 Accelerating... Current speed: 50 Braking... Current speed: 45 Braking... Current speed: 40 Braking... Current speed: 35 Braking... Current speed: 30 Braking... Current speed: 25

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int exit();

class Car
{
   private:
       int Year;
       string Make;
       int Speed;

   public:
       Car(int, string, int);
       int getYear();
       string getMake();
       int getSpeed();
       int Accelerate();
       void getDisplay();
       int Brake();
};

Car::Car(int Yr, string Mk, int Spd)
{
   Year = Yr;
   Make = Mk;
   Speed = Spd;
}

int Car::getYear()
{
   cout << "Enter the year car: ";
   cin >> Year;

   return Year;
}

string Car::getMake()
{
   cout << "Enter the make and model of the car: ";
   cin >> Make;

   return Make;
}

int Car::getSpeed()
{
   cout << "Enter the speed of the car: ";
   cin >> Speed;

   return Speed;
}

int Car::Accelerate()
{
   Speed = Speed + 5;

   return Speed;
}

int Car::Brake()
{
   Speed = Speed - 5;

   return Speed;
}

void getDisplay()
{
   int choice;
  
cout << "The car is a " Car::getYear() << " " << Car::getMake << " going " << Car::getSpeed() << " MPH." << endl;
   Car car(getYear(), getMake, getSpeed());

   do
   {
       cout << " Menu " << endl;
       cout << "-----------------------" << endl;
       cout << " 1. Accelerate " << endl;
       cout << " 2. Brake " << endl;
       cout << " 3. Exit " << endl;
       cout << "-----------------------" << endl;
       cout << " Enter your choice: " << endl;
       cin >> choice;

       switch (choice)
       {
           case 1: cout << "Accelerating";
                   cout << car.Accelerate();
                   break;
           case 2: cout << "Braking";
                   cout << car.Brake();
                   break;
           case 3: cout << "Exiting Program";
                   exit();
                   break;
       }

       while (choice < 1 || choice > 3)
       {
           cout << " Your choice must be 1-3. Re-enter. " << endl;
           cout << " Menu " << endl;
           cout << "-----------------------" << endl;
           cout << " 1. Accelerate " << endl;
           cout << " 2. Brake " << endl;
           cout << " 3. Exit " << endl;
           cout << "-----------------------" << endl;
           cout << " Enter your choice: " << endl;
           cin >> choice;
       }
   } while (choice != 3);
}

int exit()
{
   return (0);
}

int main()
{
   getDisplay();

   cout << endl << "Press ENTER to exit...";
   cin.clear();
   cin.sync();
   cin.get();