In: Computer Science
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance betweenclasses. You have to write four classes in C++ i.e. one superclass, two sub classes and one driver class. Vehicle is the super class whereas Bus and Truck are sub classesof Vehicle class. Transport is a driver class which contains mainmethod.
Detailed description of Vehicle (Super class): The class Vehicle must have following attributes: 1. Vehicle model 2. Registration number 3. Vehicle speed (km/hour) 4. Fuel capacity (liters) 5. Fuel consumption (kilo meters/liter) The Vehicle class must have following methods: 1. Parameterized constructor that will initialize all the datamembers with the given values. 2. Getters and Setters for each data member that will get and setthe values of data members of class. 3. A method fuelNeeded() that will takedistance (in kilo meter) as an argument.It will calculate the amount of fuel needed for the given distanceand will return the value of fuel needed for given distance. Youcan use the attributes ‘Fuel consumption’defined within above Vehicle class to determine the fuel needed forthe given distance. You are required to implement thisfunctionality by yourself. 4. A method distanceCovered() that willtake time (in hours) as an argument. Itwill calculate the distance for the given time and speed andreturns the value of distance. The formula to calculate speed isgiven as speed = distance/time. You can use thisformula to calculate the distance. 5. A display() method that will displayall the information of a vehicle. Detailed description of Truck (Sub class): The class Truck must have following attribute: Cargo weight limit (Kilo grams) The above class must have following methods: 1. Parameterized constructor that will initialize all data memberswith the given values. 2. Getters and setters for each data member that will get and setthe values of data members of class. 3. It must also override the display()method of Vehicle class and must call display() method of superclass within overridden method. Detailed description of Bus (Sub class): The class Bus must have following attribute: No of passengers The above class must have following methods: 1. Parameterized constructor that will initialize all the datamembers with given values. 2. Getters and setters that will get and set the value of eachdata member of class. 3. It must also override the display()method of Vehicle class and must call display method of super classwithin overridden method. Create a class Transport whichcontains the main method. Perform the following within mainmethod: Create an instance of class Truck and initialize all the datamembers with proper values. Create an instance of class Bus and initialize all the datamembers with proper values. Now, call fuelNeeded(),distanceCovered() anddisplay() methods using objects of theseclasses
Code:
#include <iostream>
using namespace std;
class Vehicle //Vehicle class
{
private: //all the data members are private and has to be manipulated using the getters and setters methods
string model, reg_no;
double speed, fuel_capacity, fuel_consumption;
public:
Vehicle(string model, string reg_no, double speed = 0, double fuel_capacity = 0, double fuel_consumption = 0) //Parametrized constructor
{
this->model = model; //using this pointer to differentiate the data members and parameters
this->reg_no = reg_no; //initialising all values
this->speed = speed;
this->fuel_capacity = fuel_capacity;
this->fuel_consumption = fuel_consumption;
}
void setModel(string model) //setter method for model of vehicle
{
this->model = model;
}
string getModel() //getter method for model of vehicle
{
return model;
}
void setRegNo(string reg_no) //setter method for registration no of vehicle
{
this->reg_no = reg_no;
}
string getRegNo() //getter method for registration no of vehicle
{
return reg_no;
}
void setSpeed(double speed) //setter method for speed of vehicle
{
this->speed = speed;
}
double getSpeed() //getter method for speed of vehicle
{
return speed;
}
void setFuelCapacity(double fuel_capacity) //setter method for fuel capacity of vehicle
{
this->fuel_capacity = fuel_capacity;
}
double getFuelCapacity() //getter method for fuel capacity of vehicle
{
return fuel_capacity;
}
void setFuelConsumption(double fuel_consumption) //setter method for fuel consumption of vehicle
{
this->fuel_consumption = fuel_consumption;
}
double getFuelConsumption() //getter method for fuel consumption of vehicle
{
return fuel_consumption;
}
double fuelNeeded(double distance) //fuel needed method that calculates the fuel required for given distance using fuel consumption
{
return distance/getFuelConsumption();
}
double distanceCovered(int time) //distance convered method takes in time and gives the distance that can be convered in that time based upon speed of vehicle
{
return speed*time;
}
void display() //displaying vehicle information
{
cout << endl;
cout << "Vehicle model: " << getModel() << endl;
cout << "Vehicle registration no: " << getRegNo() << endl;
cout << "Vehicle speed: " << getSpeed() << endl;
cout << "Vehicle fuel capacity: " << getFuelCapacity() << endl;
cout << "Vehicle fuel consumption: " << getFuelConsumption() << endl;
}
};
class Truck: public Vehicle //Declaring Truck Class inheriting from Vehicle class using public inheritance
{
private: //All the data members should be private and must be manipulated using getter and setter methods
double cargo_limit;
public:
Truck(string model, string reg_no, double speed = 0, double fuel_capacity = 0, double fuel_consumption = 0, double cargo_limit = 0)
:Vehicle(model, reg_no, speed, fuel_capacity, fuel_consumption) //Parametrized Truck constructor by calling Vehicle constructor first
{
this->cargo_limit = cargo_limit;
}
void setCargoLimit(double cargo_limit) //setter method to set cargo weight limit of truck
{
this->cargo_limit = cargo_limit;
}
double getCargoLimit() //getter method to get the cargo weight limit of truck
{
return cargo_limit;
}
void display() //displaying truck information
{
Vehicle::display(); //calling display method of vehicle (super class) before displaying info of Truck class
cout << "Truck cargo weight limit: " << getCargoLimit() << endl;
}
};
class Bus: public Vehicle //Declaring Bus Class inheriting from Vehicle class using public inheritance
{
private: //All the data members should be private and must be manipulated using getter and setter methods
int passengers_count;
public:
Bus(string model, string reg_no, double speed = 0, double fuel_capacity = 0, double fuel_consumption = 0, int passengers_count = 0)
:Vehicle(model, reg_no, speed, fuel_capacity, fuel_consumption) //Parametrized Bus constructor by calling Vehicle constructor first
{
this->passengers_count = passengers_count;
}
void setPassengersCount(int passengers_count) //setter method to set the number of passengers of the bus
{
this->passengers_count = passengers_count;
}
double getPassengersCount() //getter method to get the number of passengers of the bus
{
return passengers_count;
}
void display() //displaying bus information
{
Vehicle::display(); //calling display method of vehicle (super class) before displaying info of Bus class
cout << "Bus passengers count: " << getPassengersCount() << endl;
}
};
class Transport //Declaring Transport class
{
public:
static void main() //defining main() as class method that returns nothing
{
Truck t("Ford F-150", "12345", 96, 136, 9.41, 673); //declaring a instance of Truck class and initialising
t.display(); //displaying info of truck object
cout << "Fuel Need by truck to travel 10km: " << t.fuelNeeded(10) << endl; //calculating fuel need for 10km by truck
cout << "Distance covered by truck in 3hrs: " << t.distanceCovered(3) << endl; //calculating distance covered by truck in 3hrs
Bus b("Tata LP 912", "TS1627", 107, 160, 7.53, 36); //declaring a instance of Bus class and initialising
b.display(); //displaying info of bus object
cout << "Fuel Need by bus to travel 10km: " << b.fuelNeeded(10) << endl; //calculating fuel need for 10km by bus
cout << "Distance covered by bus in 3hrs: " << b.distanceCovered(3) << endl; //calculating distance covered by bus in 3hrs
}
};
int main() //main function
{
Transport t; //declaring a transport object
t.main(); //starting the main function of transport object
return 0;
}
Code Screenshots:
Output:
Each and everything is explained with in the comment section of the code.
Thank you! Hit like if you like my work.