Question: 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

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

  1. Ensure the file named Motorcycle.cpp is open in your editor.
  2. Create the Motorcycle class by deriving it from the Vehicle class. Use a public derivation.
  3. In the Motorcycle class, create a private attribute named sidecar. The sidecar attribute should be data type bool.
  4. Write a public set method to set the value for sidecar.
  5. Write a public get method to retrieve the value of sidecar.
  6. 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".
  7. Open the file named MyMotorcycleClassProgram.cpp.
  8. In the MyMotorcycleClassProgram, create two Motorcycle objects named motorcycleOne and motorcycleTwo.
  9. Set the sidecar value of motorcycleOne to true and the sidecar value of motorcycleTwo to false.
  10. Set motorcycleOnes maximum speed to 90 and motorcycleTwos maximum speed to 85.
  11. Set motorcycleOnes current speed to 65 and motorcycleTwos current speed to 60.
  12. Accelerate motorcycleOne by 30 mph, and accelerate motorcycleTwo by 20 mph.
  13. Print the current speed of motorcycleOne and motorcycleTwo.
  14. 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.
  15. Execute the program by clicking the Run button at the bottom of the screen.

Motorcycle.cpp

// Motorcycle.cpp

#include "Vehicle.cpp"

#include

using namespace std;

// Write the Motorcycle class here

MyMotorcycleClassProgram.cpp.

// This program uses the programmer-defined Motorcycle class.

#include "Motorcycle.cpp"

#include

using namespace std;

int main()

{

// 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;

}

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;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!