fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Base class: Vehicle
  6. class Vehicle {
  7. protected:
  8. string make;
  9. string model;
  10. int year;
  11.  
  12. public:
  13. Vehicle(string make, string model, int year): make(make), model(model), year(year) {
  14. }
  15.  
  16. virtual void displayDetails() const {
  17. cout << "Vehicle Details:\n";
  18. cout << "Make: " << make << ", Model: " << model << ", Year: " << year << endl;
  19. }
  20.  
  21. virtual ~Vehicle() {} // Virtual destructor for proper cleanup of derived class objects
  22. };
  23.  
  24. // Subclass: Car
  25. class Car : public Vehicle {
  26. int seatingCapacity;
  27.  
  28. public:
  29. Car(string make, string model, int year, int seatingCapacity)
  30. : Vehicle(make, model, year) {
  31. this->seatingCapacity = seatingCapacity;
  32. }
  33.  
  34. void displayDetails() const override {
  35. Vehicle::displayDetails();
  36. cout << "Seating Capacity: " << seatingCapacity << endl;
  37. }
  38. };
  39.  
  40. // Subclass: Truck
  41. class Truck : public Vehicle {
  42. int payloadCapacity;
  43.  
  44. public:
  45. Truck(string make, string model, int year, int payloadCapacity)
  46. : Vehicle(make, model, year) {
  47. this->payloadCapacity = payloadCapacity;
  48. }
  49.  
  50. void displayDetails() const override {
  51. Vehicle::displayDetails();
  52. cout << "Payload Capacity: " << payloadCapacity << " kg" << endl;
  53. }
  54. };
  55.  
  56. // Subclass of Car: ElectricCar
  57. class ElectricCar : public Car {
  58. int batteryCapacity; // Battery capacity in kWh
  59.  
  60. public:
  61. ElectricCar(string make, string model, int year, int seatingCapacity, int batteryCapacity)
  62. : Car(make, model, year, seatingCapacity) {
  63. this->batteryCapacity = batteryCapacity;
  64. }
  65.  
  66. void displayDetails() const override {
  67. Car::displayDetails();
  68. cout << "Battery Capacity: " << batteryCapacity << " kWh" << endl;
  69. }
  70. };
  71.  
  72. // Subclass of Truck: SemiTruck
  73. class SemiTruck : public Truck {
  74. int axleCount;
  75.  
  76. public:
  77. SemiTruck(string make, string model, int year, int payloadCapacity, int axleCount)
  78. : Truck(make, model, year, payloadCapacity) {
  79. this->axleCount = axleCount;
  80. }
  81.  
  82. void displayDetails() const override {
  83. Truck::displayDetails();
  84. cout << "Axle Count: " << axleCount << endl;
  85. }
  86. };
  87.  
  88. // Main function
  89. int main() {
  90. // Creating instances
  91. Car car("Toyota", "Corolla", 2020, 5);
  92. ElectricCar eCar("Tesla", "Model S", 2022, 5, 100);
  93. Truck truck("Ford", "F-150", 2019, 3000);
  94. SemiTruck semiTruck("Volvo", "FH16", 2021, 18000, 6);
  95.  
  96. // Display details
  97. cout << "\nCar Details:\n";
  98. car.displayDetails();
  99.  
  100. cout << "\nElectric Car Details:\n";
  101. eCar.displayDetails();
  102.  
  103. cout << "\nTruck Details:\n";
  104. truck.displayDetails();
  105.  
  106. cout << "\nSemi-Truck Details:\n";
  107. semiTruck.displayDetails();
  108.  
  109. return 0;
  110. }
  111.  
Success #stdin #stdout 0s 5180KB
stdin
Standard input is empty
stdout
Car Details:
Vehicle Details:
Make: Toyota, Model: Corolla, Year: 2020
Seating Capacity: 5

Electric Car Details:
Vehicle Details:
Make: Tesla, Model: Model S, Year: 2022
Seating Capacity: 5
Battery Capacity: 100 kWh

Truck Details:
Vehicle Details:
Make: Ford, Model: F-150, Year: 2019
Payload Capacity: 3000 kg

Semi-Truck Details:
Vehicle Details:
Make: Volvo, Model: FH16, Year: 2021
Payload Capacity: 18000 kg
Axle Count: 6