Project 3
1. Defining the Enums and Base Class:
enum Type {
GAS,
DIESEL,
ELECTRIC
};
class Vehicle {
protected:
std::string make;
std::string model;
int year;
int odometer;
public:
Vehicle(const std::string& make, const std::string& model, int year, int odometer);
virtual Type getType() const = 0;
virtual void getCarInformation() const;
void setMake(const std::string& make);
void setModel(const std::string& model);
void setYear(int year);
void setOdometer(int odometer);
const std::string& getMake() const;
const std::string& getModel() const;
int getYear() const;
int getOdometer() const;
virtual void checkTireRotation() const;
};
2. Implementing Vehicle class methods:
Vehicle::Vehicle(const std::string& make, const std::string& model, int year, int odometer)
: make(make), model(model), year(year), odometer(odometer) {}
void Vehicle::getCarInformation() const {
std::cout << "Make: " << make>
}
void Vehicle::setMake(const std::string& make) {
this->make = make;
}
void Vehicle::setModel(const std::string& model) {
this->model = model;
}
void Vehicle::setYear(int year) {
this->year = year;
}
void Vehicle::setOdometer(int odometer) {
this->odometer = odometer;
}
const std::string& Vehicle::getMake() const {
return make;
}
const std::string& Vehicle::getModel() const {
return model;
}
int Vehicle::getYear() const {
return year;
}
int Vehicle::getOdometer() const {
return odometer;
}
void Vehicle::checkTireRotation() const {
int distance = odometer P000;
if (distance >= 0) {
std::cout << "Tire rotation advised." << std>
} else {
std::cout << "Tires are good to go." << std>
}
}
3. Creating Derived Classes:
class GasCar : public Vehicle {
public:
GasCar(const std::string& make, const std::string& model, int year, int odometer);
Type getType() const override;
void getFuelType() const;
};
class DieselCar : public Vehicle {
public:
DieselCar(const std::string& make, const std::string& model, int year, int odometer);
Type getType() const override;
void getFuelType() const;
};
class ElectricCar : public Vehicle {
public:
ElectricCar(const std::string& make, const std::string& model, int year, int odometer);
Type getType() const override;
void checkChargerPower() const;
};
4. Implementing Derived Class Methods:
GasCar::GasCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type GasCar::getType() const {
return GAS;
}
void GasCar::getFuelType() const {
std::cout << "Fuel Type: Gasoline" << std>
}
DieselCar::DieselCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type DieselCar::getType() const {
return DIESEL;
}
void DieselCar::getFuelType() const {
std::cout << "Fuel Type: Diesel" << std>
}
ElectricCar::ElectricCar(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
Type ElectricCar::getType() const {
return ELECTRIC;
}
void ElectricCar::checkChargerPower() const {
int distance = odometer @0;
if (distance == 0) {
std::cout << "Battery recharge advised." << std>
} else {
std::cout << "Vehicle is good to go." << std>
}
}
5. Creating the Abstract Class for Fuel Vehicles:
class FuelVehicle : public Vehicle {
public:
FuelVehicle(const std::string& make, const std::string& model, int year, int odometer);
void checkOil() const;
virtual Type getFuelType() const = 0;
};
6. Implementing Abstract Class Methods:
FuelVehicle::FuelVehicle(const std::string& make, const std::string& model, int year, int odometer)
: Vehicle(make, model, year, odometer) {}
void FuelVehicle::checkOil() const {
// Implement oil check logic here
std::cout << "Oil check logic goes here." << std>
}
7. Creating a Library Class for Managing Vehicles:
class VehicleManager {
private:
std::vector
public:
void addVehicle(Vehicle* vehicle);
void listVehicles() const;
void updateVehicle(int index);
void removeVehicle(int index);
void tireRotation(int index);
void oilChange(int index);
void recharge(int index);
};
8. Implementing Library Class Methods:
void VehicleManager::addVehicle(Vehicle* vehicle) {
vehicles.push_back(vehicle);
}
void VehicleManager::listVehicles() const {
for (int i = 0; i < vehicles>
std::cout << "Vehicle #" << i>
vehicles[i]->getCarInformation();
vehicles[i]->getFuelType();
vehicles[i]->checkTireRotation();
std::cout << std>
}
}
void VehicleManager::updateVehicle(int index) {
// Implement update logic here
}
void VehicleManager::removeVehicle(int index) {
if (index >= 0 && index < vehicles>
delete vehicles[index];
vehicles.erase(vehicles.begin() + index);
}
}
void VehicleManager::tireRotation(int index) {
if (index >= 0 && index < vehicles>
vehicles[index]->checkTireRotation();
}
}
void VehicleManager::oilChange(int index) {
if (index >= 0 && index < vehicles>
vehicles[index]->checkOil();
}
}
void VehicleManager::recharge(int index) {
if (index >= 0 && index < vehicles>
vehicles[index]->checkChargerPower();
}
}
9. Implementing the main.cpp:
int main() {
VehicleManager manager;
while (true) {
std::cout << "Menu:\n"
<< " 0. Quit\n"
<< " 1. List inventory\n"
<< " 2. Add vehicle\n"
<< " 3. Update vehicle\n"
<< " 4. Remove vehicle\n"
<< " 5. Tire Rotation\n"
<< " 6. Oil Change\n"
<< " 7. Recharge\n"
<< "Enter your choice: ";
int choice;
std::cin >> choice;
switch (choice) {
case 0:
return 0;
case 1:
manager.listVehicles();
break;
case 2:
// Implement add vehicle logic
break;
case 3:
// Implement update vehicle logic
break;
case 4:
// Implement remove vehicle logic
break;
case 5:
// Implement tire rotation logic
break;
case 6:
// Implement oil change logic
break;
case 7:
// Implement recharge logic
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}
Program Explanation:
- Enums and Base Class: It starts by defining an enumeration Type to represent types of vehicles like GAS, DIESEL, and ELECTRIC. Then, it defines a base class Vehicle which contains common attributes and methods shared by all vehicles. These attributes include the vehicle's make, model, year, and odometer reading.
- Vehicle Class: The Vehicle class constructor initializes the vehicle's details. It also provides getter and setter methods to access and modify these details. Additionally, it has a method called checkTireRotation that advises whether the vehicle's tires need rotation based on the odometer reading.
- Derived Classes: The code defines three derived classes - GasCar, DieselCar, and ElectricCar, representing specific types of vehicles. These classes inherit from the Vehicle class and implement the pure virtual method getType to specify their fuel type. They may also include additional methods specific to their type, like getFuelType for displaying fuel type or checkChargerPower for electric cars.
- FuelVehicle Abstract Class: An abstract class FuelVehicle is defined, which inherits from Vehicle. It includes a method checkOil and declares a pure virtual function getFuelType. The checkOil method represents a common maintenance task shared among fuel-powered vehicles.
- VehicleManager Class: The VehicleManager class is created to manage a collection of vehicles. It includes methods for adding, listing, updating, removing, and performing maintenance tasks on vehicles in the inventory. This class encapsulates the logic for interacting with the vehicles.
- Main Function: The main function acts as the program's entry point. It creates an instance of the VehicleManager class and provides a menu for users to interact with the vehicle inventory. Users can list vehicles, add new ones, update vehicle information, remove vehicles, and perform maintenance tasks.