#include <iostream>
#include <string>
using namespace std;
// Base class: Vehicle
class Vehicle {
protected:
string make;
string model;
int year;
public:
Vehicle(string make, string model, int year): make(make), model(model), year(year) {
}
virtual void displayDetails() const {
cout << "Vehicle Details:\n";
cout << "Make: " << make << ", Model: " << model << ", Year: " << year << endl;
}
virtual ~Vehicle() {} // Virtual destructor for proper cleanup of derived class objects
};
// Subclass: Car
class Car : public Vehicle {
int seatingCapacity;
public:
Car(string make, string model, int year, int seatingCapacity)
: Vehicle(make, model, year) {
this->seatingCapacity = seatingCapacity;
}
void displayDetails() const override {
Vehicle::displayDetails();
cout << "Seating Capacity: " << seatingCapacity << endl;
}
};
// Subclass: Truck
class Truck : public Vehicle {
int payloadCapacity;
public:
Truck(string make, string model, int year, int payloadCapacity)
: Vehicle(make, model, year) {
this->payloadCapacity = payloadCapacity;
}
void displayDetails() const override {
Vehicle::displayDetails();
cout << "Payload Capacity: " << payloadCapacity << " kg" << endl;
}
};
// Subclass of Car: ElectricCar
class ElectricCar : public Car {
int batteryCapacity; // Battery capacity in kWh
public:
ElectricCar(string make, string model, int year, int seatingCapacity, int batteryCapacity)
: Car(make, model, year, seatingCapacity) {
this->batteryCapacity = batteryCapacity;
}
void displayDetails() const override {
Car::displayDetails();
cout << "Battery Capacity: " << batteryCapacity << " kWh" << endl;
}
};
// Subclass of Truck: SemiTruck
class SemiTruck : public Truck {
int axleCount;
public:
SemiTruck(string make, string model, int year, int payloadCapacity, int axleCount)
: Truck(make, model, year, payloadCapacity) {
this->axleCount = axleCount;
}
void displayDetails() const override {
Truck::displayDetails();
cout << "Axle Count: " << axleCount << endl;
}
};
// Main function
int main() {
// Creating instances
Car car("Toyota", "Corolla", 2020, 5);
ElectricCar eCar("Tesla", "Model S", 2022, 5, 100);
Truck truck("Ford", "F-150", 2019, 3000);
SemiTruck semiTruck("Volvo", "FH16", 2021, 18000, 6);
// Display details
cout << "\nCar Details:\n";
car.displayDetails();
cout << "\nElectric Car Details:\n";
eCar.displayDetails();
cout << "\nTruck Details:\n";
truck.displayDetails();
cout << "\nSemi-Truck Details:\n";
semiTruck.displayDetails();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gQmFzZSBjbGFzczogVmVoaWNsZQpjbGFzcyBWZWhpY2xlIHsKcHJvdGVjdGVkOgogICAgc3RyaW5nIG1ha2U7CiAgICBzdHJpbmcgbW9kZWw7CiAgICBpbnQgeWVhcjsKCnB1YmxpYzoKICAgIFZlaGljbGUoc3RyaW5nIG1ha2UsIHN0cmluZyBtb2RlbCwgaW50IHllYXIpOiBtYWtlKG1ha2UpLCBtb2RlbChtb2RlbCksIHllYXIoeWVhcikgewogICAgfQoKICAgIHZpcnR1YWwgdm9pZCBkaXNwbGF5RGV0YWlscygpIGNvbnN0IHsKICAgICAgICBjb3V0IDw8ICJWZWhpY2xlIERldGFpbHM6XG4iOwogICAgICAgIGNvdXQgPDwgIk1ha2U6ICIgPDwgbWFrZSA8PCAiLCBNb2RlbDogIiA8PCBtb2RlbCA8PCAiLCBZZWFyOiAiIDw8IHllYXIgPDwgZW5kbDsKICAgIH0KCiAgICB2aXJ0dWFsIH5WZWhpY2xlKCkge30gLy8gVmlydHVhbCBkZXN0cnVjdG9yIGZvciBwcm9wZXIgY2xlYW51cCBvZiBkZXJpdmVkIGNsYXNzIG9iamVjdHMKfTsKCi8vIFN1YmNsYXNzOiBDYXIKY2xhc3MgQ2FyIDogcHVibGljIFZlaGljbGUgewogICAgaW50IHNlYXRpbmdDYXBhY2l0eTsKCnB1YmxpYzoKICAgIENhcihzdHJpbmcgbWFrZSwgc3RyaW5nIG1vZGVsLCBpbnQgeWVhciwgaW50IHNlYXRpbmdDYXBhY2l0eSkKICAgICAgICA6IFZlaGljbGUobWFrZSwgbW9kZWwsIHllYXIpIHsKICAgICAgICB0aGlzLT5zZWF0aW5nQ2FwYWNpdHkgPSBzZWF0aW5nQ2FwYWNp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