Question: Using Inheritance to Create a Derived Class in C++ Summary In this lab, you create a derived class from a base class, and then use
Using Inheritance to Create a Derived Class in C++
Summary
In this lab, you create a derived class from a base class, and then use the derived class in a C++ program. The program should create two Motorcycle objects, and then set the Motorcycles speed, accelerate the Motorcycle object, and check its sidecar status.
Instructions
Ensure the file named Motorcycle.cpp is open in your editor.
Create the Motorcycle class by deriving it from the Vehicle class. Use a public derivation.
In the Motorcycle class, create a private attribute named sidecar. The sidecar attribute should be data type bool.
Write a public set method to set the value for sidecar.
Write a public get method to retrieve the value of sidecar.
Write a public accelerate() method. This method overrides the accelerate() method inherited from the Vehicle class. Change the message in the accelerate() method so the following is displayed when the Motorcycle tries to accelerate beyond its maximum speed: "This motorcycle cannot go that fast".
Open the file named MyMotorcycleClassProgram.cpp.
In the MyMotorcycleClassProgram, create two Motorcycle objects named motorcycleOne and motorcycleTwo.
Set the sidecar value of motorcycleOne to true and the sidecar value of motorcycleTwo to false.
Set motorcycleOnes maximum speed to 90 and motorcycleTwos maximum speed to 85.
Set motorcycleOnes current speed to 65 and motorcycleTwos current speed to 60.
Accelerate motorcycleOne by 30 mph, and accelerate motorcycleTwo by 20 mph.
Print the current speed of motorcycleOne and motorcycleTwo.
Determine if motorcycleOne and motorcycleTwo have sidecars. If yes, display the following: This motorcycle has a side car.If not, display the following: This motorcycle does not have a side car.
Execute the program by clicking the Run button at the bottom of the screen.
// Motorcycle.cpp #include "Vehicle.cpp" #include
// Write the Motorcycle class here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
