Question
Compose a C++ console program that defines a Car class and then uses Car objects in a simple physics simulation. Create a Car class inside
Compose a C++ console program that defines a Car class and then uses Car objects in a simple physics simulation.
Create a Car class inside a separate file (or more than one file if you choose to separate the interface from the implementation) with private data fields representing the car's make (for example "Ford"), the model year, the velocity of the car, and the car's x-position. The class should define a 2-arg constructor that accepts the make and year as parameters and initializes the velocity and position to 0. Provide accessor methods for all data fields.
Create two mutator methods which modify the velocity, one to accelerate the car and one to decelerate the car. Each method should take the change in velocity as an input argument. The acceleration method should validate the change in velocity as positive and the deceleration method should validate the change in velocity to be negative. Print an error message if the change in velocity is invalid.
Create an additional method named updatePosition which should define a parameter to accept the amount of time that has elapsed in the simulation. The distance traveled can be calculated by multiplying the velocity by the elapsed time. The car's new position can be calculated by adding the distance traveled to the car's current position.
From main, begin by allowing the user to provide input and use that input to fill a vector with 5 car objects.
After creating all of the cars, use a loop to provide a simple menu system with the following options:
- Accelerate
- Decelerate
- Simulate Time Passing
- Print Simulation
- Exit
The accelerate and decelerate menu options should prompt the user for which car to change the velocity of. The simulate option should prompt the user for how much to time to pass. The print option should print the state of all cars and finally print which car is leading the race!
Note: If using an array, you will need to add a zero-arg constructor when declaring an array of objects because the objects are constructed immediately. You would then need to provide mutator methods for a few of the data members in order to allow the user to initialize them.
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