Question: Using Inheritance to Create a Derived Class in C++: In this lab, you create a derived class from a base class, and then use the
Using Inheritance to Create a Derived Class in C++:
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.
Correctly ouputs the current speed
0 out of 1 checks passed. Review the results below for more details.
Checks
Test CaseIncomplete
Test output for current speed
Input
Output
This motorcycle cannot go that fast. MotorcycleOne speed:65 MotorcycleTwo speed:80 MotorcycleOne has a sidecar MotorcycleTwo does not have a sidecar
Results
The current speed of motorcycleOne is 65
The current speed of motorcycleTwo is 80
out of 10.00
Correctly outputs if motorcycle has a sidecar
0 out of 1 checks passed. Review the results below for more details.
Checks
Test CaseIncomplete
Test output for Sidecar status
Input
Output
This motorcycle cannot go that fast. MotorcycleOne speed:65 MotorcycleTwo speed:80 MotorcycleOne has a sidecar MotorcycleTwo does not have a sidecar
Results
This motorcycle has a side car
This motorcycle does not have a side car
Vehicle.cpp:
// Vehicle.cpp
#include
using namespace std;
class Vehicle
{
public:
void setSpeed(double);
double getSpeed();
void accelerate(double);
void setFuelCapacity(double);
double getFuelCapacity();
void setMaxSpeed(double);
double getMaxSpeed();
private:
double fuelCapacity;
double maxSpeed;
double currentSpeed;
};
void Vehicle::setSpeed(double speed)
{
currentSpeed = speed;
return;
}
double Vehicle::getSpeed()
{
return currentSpeed;
}
void Vehicle::accelerate(double mph)
{
if(currentSpeed + mph < maxSpeed)
currentSpeed = currentSpeed + mph;
else
cout << "This vehicle cannot go that fast." << endl;
}
void Vehicle::setFuelCapacity(double fuel)
{
fuelCapacity = fuel;
}
double Vehicle::getFuelCapacity()
{
return fuelCapacity;
}
void Vehicle::setMaxSpeed(double max)
{
maxSpeed = max;
}
double Vehicle::getMaxSpeed()
{
return maxSpeed;
}
MyMotorcycleClassProgram.cpp:
#include "Motorcycle.cpp"
#include
using namespace std;
int main()
{
Motorcycle motorcycleOne,motorcycleTwo;
motorcycleOne.setSidecar(true);
motorcycleTwo.setSidecar(false);
motorcycleOne.setMaxSpeed(90);
motorcycleTwo.setMaxSpeed(85);
motorcycleOne.setSpeed(65);
motorcycleTwo.setSpeed(60);
motorcycleOne.accelerate(30);
motorcycleTwo.accelerate(20);
cout<<"MotorcycleOne speed:"<< motorcycleOne.getSpeed()< cout<<"MotorcycleTwo speed:"<< motorcycleTwo.getSpeed()< if(motorcycleOne.getSidecar()) cout<<"MotorcycleOne has a sidecar"< else cout<<"MotorcycleOne does not have a sidecar"< if(motorcycleTwo.getSidecar()) cout<<"MotorcycleTwo has a sidecar"< else cout<<"MotorcycleTwo does not have a sidecar"< // Create Motorcyle objects here // Create a boolean variable for side car status // Set side car status here // Set maximum speed here // Set current speed here // Accelerate motorcyles here // Display current speed here // Determine side car status and display results. return 0; } Motorcycle.cpp: #include "Vehicle.cpp" #include using namespace std; class Motorcycle:public Vehicle{ public: void setSidecar(bool); bool getSidecar(); void accelerate(double); private: bool sidecar; }; void Motorcycle::setSidecar(bool s){ sidecar=s; } bool Motorcycle::getSidecar(){ return sidecar; } void Motorcycle::accelerate(double mph) { if(getSpeed() + mph < getMaxSpeed()) setSpeed(getSpeed() + mph); else cout << "This motorcycle cannot go that fast." << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
