73.3k views
4 votes
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 Motorcycle’s speed, accelerate the Motorcycle object, and check its sidecar status.

1 Answer

7 votes

Answer:

See explaination

Step-by-step explanation:

Program source code below.

#include "Motorcycle.cpp"

#include <iostream>

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()<<endl;

cout<<"MotorcycleTwo speed:"<< motorcycleTwo.getSpeed()<<endl;

if(motorcycleOne.getSidecar())

cout<<"MotorcycleOne has a sidecar"<<endl;

else

cout<<"MotorcycleOne does not have a sidecar"<<endl;

if(motorcycleTwo.getSidecar())

cout<<"MotorcycleTwo has a sidecar"<<endl;

else

cout<<"MotorcycleTwo does not have a sidecar"<<endl;

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

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;

}

User Zehava
by
4.3k points