Challenge 31

Description

The purpose of this challenge is to write a basic class with member functions and variables. This challenge illustrates the use of various functions and constructors.

Requirements

  1. Note that, typically, writing cout statements in classes are a bad idea. This exercise does employ the use of cout statements within the class to illustrate how the functions are being used and called.
  2. Create a class called Engine
  3. In Engine, create the following private properties:
    1. int rpm, speed
    2. int drive_mode, rpm_gain, max_speed
    3. string drive_modes[4]
    4. string filename
  4. Write a private function set_drive_modes(). In this function, simply fill the array drive_modes with the values “Eco Pro”, “Comfort”, “Sport”, “Sport Plus”. Make sure you do not redeclare/recreate a new drive_modes array in this function. The values of 0, 1, 2, 3 will be assigned later to drive_mode and will respectively represent the string values in the drive_modes array.
  5. Write a constructor that takes in a single string parameter. Remember that constructors have the same name as the class, do not have a return type and must be public. Set the private variable filename to the string parameter of the constructor.
  6. Write a private function int load_drive_mode(). This function will open a file whose filename is stored in the filename private variable. In this file, you expect to read a single integer value. Read this value and return it out of the function. If the file doesn’t exist, return a value of 1.
  7. Write a private function void save_drive_mode(). This function will save the value of the private variable drive_mode into a file whose name is stored in filename. Also, when saving drive_mode, include a line terminator (endl) to ensure that this token can be read later.
  8. Write a private function prep_engine(). This function will set the values of rpm_gain and max_speed based on the value of drive_mode as below:
    1. When drive_mode is 0, set rpm_gain to 100, set max_speed to 65
    2. When drive_mode is 1, set rpm_gain to 300, set max_speed to 85
    3. When drive_mode is 2, set rpm_gain to 1000, set max_speed to 185
    4. When drive_mode is 3, set rpm_gain to 1500, set max_speed to 185
  9. In your constructor, add the following functionality
    1. call set_drive_modes() function
    2. set drive_mode to the value returned by calling load_drive_mode().
    3. call prep_engine() function
    4. set rpm to 0
    5. set speed to 0
  10. Write a public function void accelerate(). This function, every time it’s called will increase rpm by rpm_gain and speed by 10. speed cannot exceed max_speed.
  11. Write a public function void decelerate(). This function, every time it’s called will decrease rpm by 200 and speed by 10. speed cannot go below 0 and rpm cannot go below 0.
  12. Write a public function void increase_drive_mode(). This function will increment the value of drive_mode by 1, not to exceed 3. This function will call prep_engine() and save_drive_mode() after the adjustment of drive_mode.
  13. Write a public function void lower_drive_mode(). This function will decrement the value of drive_mode by 1not to go below 0. This function will call prep_engine() and save_drive_mode() after the adjustment of drive_mode.
  14. Write a public function void show_status(). This function will display the status of drive_mode, rpm and speed 
  15. Finally, in main(), write out the main below which will essentially use the class and its functions.

Sample main()

int main()
{
  Engine engine("bmwx3");
  char action;
  
  do
  {
    cout << "A - accelerate\n";
    cout << "D - decelerate\n";
    cout << "M - increase drive mode\n";
    cout << "N - lower drive mode\n";
    cout << "Q - park the car (quit)\n";
    cout << "Action? ";
    cin >> action;

    if (action == 'a')
      engine.accelerate();
    else if (action == 'd')
      engine.decelerate();
    else if (action == 'm')
      engine.increase_drive_mode();
    else if (action == 'n')
      engine.lower_drive_mode();

    engine.show_status();

  } while (action != 'q');
  
  return 0;
}

Sample Interaction

A - accelerate
D - decelerate
M - increase drive mode
N - lower drive mode
Q - park the car (quit)

Action? m

Dashboard: Drive Mode: Sport, Speed: 0, RPM: 0

A - accelerate
D - decelerate
M - increase drive mode
N - lower drive mode
Q - park the car (quit)

Action? a

Dashboard: Drive Mode: Sport, Speed: 10, RPM: 1000  

A - accelerate
D - decelerate
M - increase drive mode
N - lower drive mode
Q - park the car (quit) 

Action? a

Dashboard: Drive Mode: Comfort, Speed: 20, RPM: 1300 

A - accelerate
D - decelerate
M - increase drive mode
N - lower drive mode
Q - park the car (quit) 

Action? a

Dashboard: Drive Mode: Comfort, Speed: 30, RPM: 1800

A - accelerate
D - decelerate
M - increase drive mode
N - lower drive mode
Q - park the car (quit) 

Action? a

Dashboard: Drive Mode: Comfort, Speed: 40, RPM: 2100

>> run it a few times pressing various choices to ensure that drivem modes change and speeds do not exceed the max rated speed for the various drive modes

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0031

Print Requirements