Project 2
Solution-
Code for the problem
-
First, we include the necessary header files, which include iostream, string, vector, and algorithm. (These are those files that store predefined functions. It contains definitions of functions that you can include or import using a preprocessor directive #include. This preprocessor directive tells the compiler that the header file needs to be processed prior to the compilation.)
-
We define an enum called Type that includes GAS, DIESEL, and ELECTRIC.(enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex)
-
We create a base class called Vehicle that includes protected member variables such as Make, Model, Year, and Odometer. We define a constructor and getter and setter functions for these member variables. We also define a pure virtual function called getType().( a base class is an existing class from which the other classes are determined and properties are inherited)
-
We create three derived classes called GasCar, DieselCar, and ElectricCar that inherit from the Vehicle class. These classes define their own getType() functions and getFuelType() functions specific to their fuel type.(A derived class is a class that is constructed from a base class or an existing class. It has a tendency to acquire all the methods and properties of a base class. It is also known as a subclass or child class)
-
We create an abstract class called FuelVehicle that inherits from the Vehicle class. This class defines a checkOil() function and a pure virtual function called getFuelType().(An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function)
-
We implement the maintenance functions checkTireRotation(), checkPower(), and oilChange() in the ElectricCar class, which will be called based on the odometer reading.
-
We define a class called Inventory that includes a vector of Vehicle pointers called inventoryList. We also define functions for adding, updating, listing, and removing vehicles from the inventory.(A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions as its members whose access is governed by the three access specifiers private, protected or public. By default access to members of a C++ class is private).
-
In the main function, we create an instance of the Inventory class and prompt the user to select an option from a menu. We then call the corresponding function based on the user's input.
-
When the user selects the option to add a new vehicle, we prompt the user for information about the vehicle and create an instance of the appropriate derived class based on the fuel type.
-
When the user selects the option to list the inventory, we iterate through the vector of vehicles and output the details of each vehicle.
-
When the user selects the option to update a vehicle, we prompt the user for the index of the vehicle to update and the new information for the vehicle.
-
When the user selects the option to remove a vehicle, we prompt the user for the index of the vehicle to remove and remove it from the vector of vehicles.
-
When the user selects the option to check tire rotation, we prompt the user for the index of the vehicle and check if the tires need rotation based on the odometer reading.
-
When the user selects the option to check for an oil change, we prompt the user for the index of the vehicle and check if an oil change is needed based on the odometer reading.
-
When the user selects the option to check for a recharge, we prompt the user for the index of the vehicle and check if a recharge is needed based on the odometer reading.
-
If the user enters an invalid option, we prompt them to enter a valid option.
-
Once the user selects the "Quit" option, we exit the program.

