Challenge

Project 3

Ans:1-

Program:-

#include

#include

#include

enum class FuelType {

    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)

        : make(make), model(model), year(year), odometer(odometer) {}

    // Getters and Setters

    std::string getMake() const { return make; }

    std::string getModel() const { return model; }

    int getYear() const { return year; }

    int getOdometer() const { return odometer; }

    void setMake(const std::string& make) { this->make = make; }

    void setModel(const std::string& model) { this->model = model; }

    void setYear(int year) { this->year = year; }

    void setOdometer(int odometer) { this->odometer = odometer; }

    // Virtual functions for maintenance tasks

    virtual void checkTireRotation() = 0;

    virtual void checkPower() = 0;

    virtual FuelType getType() const = 0;

    // Function to get car information

    void getCarInformation() const {

        std::cout << "Make: " << make << std::endl;

        std::cout << "Model: " << model << std::endl;

        std::cout << "Year: " << year << std::endl;

        std::cout << "Odometer: " << odometer << " km" << std::endl;

    }

};

class GasCar : public Vehicle {

public:

    GasCar(const std::string& make, const std::string& model, int year, int odometer)

        : Vehicle(make, model, year, odometer) {}

    // Implement virtual functions for maintenance tasks

    void checkTireRotation() override {

        int distance = odometer % 50000;

        if (distance >= 0) {

            std::cout << "Gas car: It's time to rotate the tires." << std::endl;

        } else {

            std::cout << "Gas car: Tires are good to go." << std::endl;

        }

    }

    void checkPower() override {

        std::cout << "Gas car: No need to recharge power." << std::endl;

    }

    FuelType getType() const override { return FuelType::GAS; }

};

class DieselCar : public Vehicle {

public:

    DieselCar(const std::string& make, const std::string& model, int year, int odometer)

        : Vehicle(make, model, year, odometer) {}

    // Implement virtual functions for maintenance tasks

    void checkTireRotation() override {

        int distance = odometer % 50000;

        if (distance >= 0) {

            std::cout << "Diesel car: It's time to rotate the tires." << std::endl;

        } else {

            std::cout << "Diesel car: Tires are good to go." << std::endl;

        }

    }

    void checkPower() override {

        std::cout << "Diesel car: No need to recharge power." << std::endl;

    }

    FuelType getType() const override { return FuelType::DIESEL; }

};

class ElectricCar : public Vehicle {

public:

    Electric

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.