Challenge

Project 3

Answer:

code:

#include
#include
using namespace std;

enum Type {
GAS,
DIESEL,
ELECTRIC
};

class Vehicle {
protected:
string make;
string model;
int year;
int odometer;

public:
Vehicle(string make_, string model_, int year_, int odometer_)
: make(make_), model(model_), year(year_), odometer(odometer_) {}

// Setter method for the make value
void setMake(string make_) {
make = make_;
}

// Getter method for the make value
string getMake() const {
return make;
}

// Setter method for the model value
void setModel(string model_) {
model = model_;
}

// Getter method for the model value
string getModel() const {
return model;
}

// Setter method for the year value
void setYear(int year_) {
year = year_;
}

// Getter method for the year value
int getYear() const {
return year;
}

// Setter method for the odometer value
void setOdometer(int odometer_) {
odometer = odometer_;
}

// Getter method for the odometer value
int getOdometer() const {
return odometer;
}

// Pure virtual function to get the vehicle type
virtual Type getType() = 0;

// virtual checkTireRotation()=0;

// Pure virtual function to get the vehicle information
virtual void getCarInformation() = 0;

};
// Define a class GasCar that inherits from the Vehicle class

class GasCar : public Vehicle {
public:
// Constructor for GasCar class that calls the constructor of the parent class and passes the
//arguments to it
GasCar(string make_, string model_, int year_, int odometer_)
: Vehicle(make_, model_, year_, odometer_) {}

// Function that returns the fuel type of the car as GAS
Type getType() {
return GAS;
}

// Function that checks if the tires of the car need to be rotated based on the odometer reading

// Function that prints the make, model, year, odometer reading, and fuel type of the car
void getCarInformation() {
cout <<"Make:"<< make>cout<<"Model:"<cout<<"Year:"<cout<<"Odometer reading:"<cout<<"Fuel type:Gas" <}
// Function to check for oilchange

};

// This class represents a Diesel Car and inherits from the Vehicle class

class DieselCar : public Vehicle {

public:
// Constructor for DieselCar class
DieselCar(string make_, string model_, int year_, int odometer_)
// Call the constructor of the base class 'Vehicle'
: Vehicle(make_, model_, year_, odometer_) {}

// Returns the type of the vehicle, which is Diesel for DieselCar
Type getType() {
return DIESEL;
}
// Prints the information of the diesel car
void getCarInformation() {
cout<<"Make:"<cout<<"Model:"<cout<<"Year:"<cout<<"Odometer reading:"<cout<<"Fuel type:Diesel"<}
};
class Electriccar : public Vehicle {

public:
// Constructor for DieselCar class
Electriccar(string make_, string model_, int year_, int odometer_)
// Call the constructor of the base class 'Vehicle'
: Vehicle(make_, model_, year_, odometer_) {}

// Returns the type of the vehicle, which is Diesel for DieselCar
Type getType() {
return ELECTRIC;
}
// Prints the information of the diesel car
void getCarInformation() {
cout<<"Make:"<cout<<"Model:"<cout<<"Year:"<cout<<"Odometer reading:"<cout<<"Fuel type:Diesel"<}
};
int main() {
vector vehicles;
GasCar gasCar1("Toyota", "Corolla", 2022, 15000);
DieselCar dieselCar1("Ford", "F-150", 2021, 20000);
vehicles.push_back(&gasCar1);
vehicles.push_back(&dieselCar1);
string make, model;
int year, odometer;
cout<<"Enter Gas Car Make:";
cin >>make;
cout<<"Enter Gas Car Model:";
cin>>model;
cout<<"Enter Gas Car Year:";
cin>>year;
cout<<"Enter Gas Car Odometer Reading:";
cin>>odometer;
GasCar* gasCar2 = new GasCar("","", 0, 0);
gasCar2->setMake(make);
gasCar2->setModel(model);
gasCar2->setYear(year);
gasCar2->setOdometer(odometer);
vehicles.push_back(gasCar2);
cout<<"Enter Diesel Car Make:";
cin>>make;
cout<<"Enter Diesel Car Model:";
cin>>model;
cout<<"Enter Diesel Car Year:";
cin>>year;
cout<<"Enter Diesel Car Odometer Reading:";
cin>>odometer;
DieselCar* dieselCar2 = new DieselCar("", "", 0, 0);
dieselCar2->setMake(make);
dieselCar2->setModel(model);
dieselCar2->setYear(year);
dieselCar2->setOdometer(odometer);
vehicles.push_back(dieselCar2);
cout<<"Enter Electric Car Make:";
cin>>make;
cout<<"Enter Electric Car Model:";
cin>>model;
cout<<"Enter Electric Car Year:";
cin>>year;
cout<<"Enter Electric Car Odometer Reading:";
cin>>odometer;
Electriccar* electriccar2 = new Electriccar("", "", 0, 0);
electriccar2->setMake(make);
electriccar2->setModel(model);
electriccar2->setYear(year);
electriccar2->setOdometer(odometer);
vehicles.push_back(electriccar2);
for (Vehicle* vehicle : vehicles) {
vehicle->getCarInformation();
cout<<"Type:"<< vehicle>getType()<< endl>cout<<"------------------------------------"<}
return 0;
}

Output:

Enter Gas Car Make:BENZ
Enter Gas Car Model:GLC
Enter Gas Car Year:2015
Enter Gas Car Odometer Reading:480000
Enter Diesel Car Make:BENZ
Enter Diesel Car Model:GLS
Enter Diesel Car Year:2006
Enter Diesel Car Odometer Reading:80000
Enter Electric Car Make:BENZ
Enter Electric Car Model:EQS
Enter Electric Car Year:2021
Enter Electric Car Odometer Reading:18000
Make:Toyota
Model:Corolla
Year:2022
Odometer reading:15000
Fuel type:Gas
Type:0
------------------------------------
Make:Ford
Model:F-150
Year:2021
Odometer reading:20000
Fuel type:Diesel
Type:1
------------------------------------
Make:BENZ
Model:GLC
Year:2015
Odometer reading:48000
Fuel type:Gas
Type:0
------------------------------------
Make:BENZ
Model:GLS
Year:2006
Odometer reading:80000
Fuel type:Diesel
Type:1
------------------------------------
Make:BENZ
Model:EQS
Year:202
Odometer reading:18000
Fuel type:Diesel
Type:2
------------------------------------

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.