Write a class named TransportationMode with two member varia
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.
Answer:
Solution
#include <iostream>
using namespace std;
class TransportationMode
{
private:
int wheels;
float mph;
public:
TransportationMode()
{
wheels = 0;
mph = 0;
}
TransportationMode(int w, float m)
{
wheels = w;
mph = m;
//cout<<\"in constructor\"<<endl;
//cout<<wheels<<\"------\"<<endl;
//cout<<mph<<\"------\"<<endl;
}
float timeToDestination(float miles)
{
float timetodest;
cout<<miles<<\"----\"<<mph<<endl;
timetodest = miles/mph;
//cout<<timetodest<<\"**************\"<<endl;
return timetodest;
}
void sayModeOfTransportation()
{
cout<<\"I am traveling on an unknown mode of transportation.\"<<endl;
}
void sayModeOfTransportation(int n)
{
if( n == 2)
cout<<\"I am traveling on a bike. \"<<endl;
if( n == 4)
cout<<\"I am traveling on a car. \"<<endl;
}
friend int getWheels(TransportationMode &obj);
};
int getWheels(TransportationMode &obj)
{
//cout<<\"ingetwheels\"<<obj.wheels<<endl;
return obj.wheels;
}
class car: public TransportationMode
{
public:
car();
car(int w, float m)
{
TransportationMode(w,m);
cout<<\"w,m\"<<w<<m<<endl;
}
};
class bike: public TransportationMode
{
public:
bike();
bike(int w, float m)
{
TransportationMode(w,m);
cout<<\"w,m\"<<w<<m<<endl;
}
};
int main() {
// your code goes here
TransportationMode obj_car(4,60.0);
TransportationMode obj_bike(2,15.0);
int wheel1, wheel2;
int miles;
wheel1 = getWheels(obj_car);
wheel2 = getWheels(obj_bike);
cout<<wheel1<<endl;
//cout<<wheel2<<endl;
obj_bike.sayModeOfTransportation(2);
obj_car.sayModeOfTransportation(4);
//cout<<\"Enter the total miles to destination \"<<endl;
//cin>>miles;
cout<<\"Time to destination using car\"<< obj_car.timeToDestination(120)<<endl;
cout<<\"Time to destination using bike\"<< obj_bike.timeToDestination(120)<<endl;
return 0;
}


