Question
In C++ Note that, typically, writing cout statements in classes are a bad idea. This exercise does employ the use of cout statements within the
In C++
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.
Create a class called Engine
In Engine, create the following private properties:
int rpm, speed
int drive_mode, rpm_gain, max_speed
string drive_modes[4]
string filename
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.
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.
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 doesnt exist, return a value of 1.
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.
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:
When drive_mode is 0, set rpm_gain to 100, set max_speed to 65
When drive_mode is 1, set rpm_gain to 300, set max_speed to 85
When drive_mode is 2, set rpm_gain to 1000, set max_speed to 185
When drive_mode is 3, set rpm_gain to 1500, set max_speed to 185
In your constructor, add the following functionality
call set_drive_modes() function
set drive_mode to the value returned by calling load_drive_mode().
call prep_engine() function
set rpm to 0
set speed to 0
Write a public function void accelerate(). This function, every time its called will increase rpm by rpm_gain and speed by 10. speed cannot exceed max_speed.
Write a public function void decelerate(). This function, every time its called will decrease rpm by 200 and speed by 10. speed cannot go below 0 and rpm cannot go below 0.
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.
Write a public function void lower_drive_mode(). This function will decrement the value of drive_mode by 1, not to go below 0. This function will call prep_engine() and save_drive_mode() after the adjustment of drive_mode.
Write a public function void show_status(). This function will display the status of drive_mode, rpm and speed
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 "; cout << "D - decelerate "; cout << "M - increase drive mode "; cout << "N - lower drive mode "; cout << "Q - park the car (quit) "; 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started