Question

In: Computer Science

Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance...

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

Solutions

Expert Solution

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.


Related Solutions

Overview In this assignment you are required to implement binary code comparator using Xilinx that it...
Overview In this assignment you are required to implement binary code comparator using Xilinx that it is compatible with the MXK Seven Segment Displays. You will draw your digital logic circuit using Xilinx and then simulate it to verify the functionality of your design. Software Requirements ? Xilinx ISE 10.1 or higher Specifications Binary Code Comparator The binary code comparator is to be implemented and made compatible with the seven 7-segment displays of the board. Represent the first five digits...
In this project, you are required to write the java program “IO.java” to implement integer operations...
In this project, you are required to write the java program “IO.java” to implement integer operations “+”, “−”, “*”. Specifically, your program reads operands from a file named “input.txt” (which will be manually placed under the directory of the program) as strings. Then your program converts strings to integers, and computes the addition, subtraction, and multiplication of the operands. Finally, your program creates a file named “output.txt” (under the directory of the program) and writes the results to the file....
Write a Verilog code to implement 16 bit LFSR
Write a Verilog code to implement 16 bit LFSR
Instructions: You are givne main.c and exam.h. Your task is to implement exam.c. write the code...
Instructions: You are givne main.c and exam.h. Your task is to implement exam.c. write the code for these 20 functions on repl.it. */ #include #include "exam/exam.h" int main(void) { return 0; } /* /* exam.h */ #ifndef _EXAM_H_ #define _EXAM_H_ // 1 // Display title of code and your name. ( koorsh maghsoodi my name) // See exam.txt for sample output. void exam_hello_world(void); // 2 // Display argc and all argv[] values. // See exam.txt for sample output. // returns:...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
Apply The knowledge of “Mapping Models to code” Concept to write Chunks of java code which...
Apply The knowledge of “Mapping Models to code” Concept to write Chunks of java code which maps the following relationships: a. Aggregation b. Composition c. Generalization d. Realization e. Dependency
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write Algoritm , code and output. In Operating Systems , . Implement a program for page...
Write Algoritm , code and output. In Operating Systems , . Implement a program for page replacement using the following a.FIFO b.LRU c.OPTIMAL
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT