fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Base class Vehicle
  7. class Vehicle {
  8. protected:
  9. int numberOfWheels;
  10. double range;
  11.  
  12. public:
  13. Vehicle(int wheels, double range) : numberOfWheels(wheels), range(range) {}
  14.  
  15. virtual void display() const {
  16. cout << "Number of Wheels: " << numberOfWheels << ", Range: " << range << " km" << endl;
  17. }
  18. };
  19.  
  20. // Derived class Car
  21. class Car : public Vehicle {
  22. int numberOfPassengers;
  23.  
  24. public:
  25. Car(int wheels, double range, int passengers)
  26. : Vehicle(wheels, range), numberOfPassengers(passengers) {}
  27.  
  28. void display() const override {
  29. Vehicle::display();
  30. cout << "Number of Passengers: " << numberOfPassengers << endl;
  31. }
  32. };
  33.  
  34. // Derived class Truck
  35. class Truck : public Vehicle {
  36. double loadLimit;
  37.  
  38. public:
  39. Truck(int wheels, double range, double load)
  40. : Vehicle(wheels, range), loadLimit(load) {}
  41.  
  42. void display() const override {
  43. Vehicle::display();
  44. cout << "Load Limit: " << loadLimit << " kg" << endl;
  45. }
  46. };
  47.  
  48. int main() {
  49. // Creating instances of Car and Truck
  50. Car myCar(4, 500, 5);
  51. Truck myTruck(6, 800, 10000);
  52.  
  53. // Displaying information
  54. cout << "Car Information:" << endl;
  55. myCar.display();
  56. cout << endl;
  57.  
  58. cout << "Truck Information:" << endl;
  59. myTruck.display();
  60.  
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Car Information:
Number of Wheels: 4, Range: 500 km
Number of Passengers: 5

Truck Information:
Number of Wheels: 6, Range: 800 km
Load Limit: 10000 kg