C++ question, Please help me figure this out, thank You! Write a class named \'T
ID: 3579479 • Letter: C
Question
C++ question, Please help me figure this out, thank You!
Write a class named 'TransportationMode' with two member variables that are NOT public, int wheels, and float mph.
Include necessary Constructors and two functions:
float timeToDestination(float miles); // Returns a float in terms of hours of how long it takes to travel given the current mode of transportations mph
void sayModeOfTransportation(); --> Should say exactly "I am traveling on an unknown mode of transportation."
Write two more classes, Car and Bike that inherit from TransportationMode.
These should have no member variables. Make the constructor for each such that Bikes have 2 wheels and travel at 15.0 mph, and cars have 4 wheels and travel at 60.0 mph.
Each of the derived class should overload the function 'void sayModeOfTransportation()' and instead cout..
"I am traveling on a bike."
"I am traveling in a car."
Don't overload the timeToDestination method.
Finally, the TransportationMode class should have a friend function named getWheels that takes a TransportationMode object as a parameter. You should declare this inside the class BUT as is the case with a friend function, you should define it outside of the class. You should not create any getter or setter functions to accomplish this.
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
class TransportMode
{
int wheels;
float mph;
public:
TransportMode()
{
wheels = 0;
mph = 0;
}
TransportMode(int w,float s)
{
wheels = w;
mph = s;
}
float timeToDestination(float miles)
{
float time;
//calculate time to destination
time = miles / mph;
return time;
}
//define sayModeOfTransportation to be virtual to make it overloaded in derived classes
virtual void sayModeOfTransportation()
{
}
};
class Car : public TransportMode
{
public:
//default constructor
Car() :TransportMode(4, 60.0)
{
}
void sayModeOfTransportation()
{
cout << "I am traveling in a car." << endl;
}
};
class Bike : public TransportMode
{
public:
//default constructor
Bike() :TransportMode(2, 15.0)
{
}
void sayModeOfTransportation()
{
cout << "I am traveling on a bike." << endl;
}
};
int main()
{
//create object of classes Car and Bike
Car car;
Bike bike;
//variable to hold destination
float distance;
cout << "Enter the distance to distination in miles: ";
cin >> distance;
//dispay mode of transport and calculate time to destination for car object
//float value displayed to 2 decimal point using setprecission
setprecision(2);
car.sayModeOfTransportation() ;
cout << "Time to destination: "<<setprecision(2) << car.timeToDestination(distance) << endl;
//dispay mode of transport and calculate time to destination for bike object
bike.sayModeOfTransportation();
cout << "Time to destination: " << setprecision(2)<<bike.timeToDestination(distance) << endl;
}
-----------------------------------------------------------------------------
output:
Enter the distance to distination in miles: 200
I am traveling in a car.
Time to destination: 3.3
I am traveling on a bike.
Time to destination: 13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.