fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // Base class Vehicle
  7. class Vehicle {
  8. protected:
  9. double mileage;
  10. double price;
  11.  
  12. public:
  13. Vehicle(double m, double p) : mileage(m), price(p) {}
  14.  
  15. virtual void display() const {
  16. cout << "Mileage: " << mileage << " km/l, Price: $" << price << endl;
  17. }
  18. };
  19.  
  20. // Subclass Car
  21. class Car : public Vehicle {
  22. protected:
  23. double ownershipCost;
  24. int warranty;
  25. int seatingCapacity;
  26. string fuelType;
  27.  
  28. public:
  29. Car(double m, double p, double oc, int w, int sc, const string& ft)
  30. : Vehicle(m, p), ownershipCost(oc), warranty(w), seatingCapacity(sc), fuelType(ft) {}
  31.  
  32. void display() const override {
  33. Vehicle::display();
  34. cout << "Ownership Cost: $" << ownershipCost << ", Warranty: " << warranty
  35. << " years, Seating Capacity: " << seatingCapacity << ", Fuel Type: " << fuelType << endl;
  36. }
  37. };
  38.  
  39. // Subclass Bike
  40. class Bike : public Vehicle {
  41. protected:
  42. int numCylinders;
  43. int numGears;
  44. string coolingType;
  45. string wheelType;
  46. double fuelTankSize;
  47.  
  48. public:
  49. Bike(double m, double p, int nc, int ng, const string& ct, const string& wt, double fts)
  50. : Vehicle(m, p), numCylinders(nc), numGears(ng), coolingType(ct), wheelType(wt), fuelTankSize(fts) {}
  51.  
  52. void display() const override {
  53. Vehicle::display();
  54. cout << "Number of Cylinders: " << numCylinders << ", Number of Gears: " << numGears
  55. << ", Cooling Type: " << coolingType << ", Wheel Type: " << wheelType
  56. << ", Fuel Tank Size: " << fuelTankSize << " inches" << endl;
  57. }
  58. };
  59.  
  60. // Subclass Audi of Car
  61. class Audi : public Car {
  62. string modelType;
  63.  
  64. public:
  65. Audi(double m, double p, double oc, int w, int sc, const string& ft, const string& mt)
  66. : Car(m, p, oc, w, sc, ft), modelType(mt) {}
  67.  
  68. void display() const override {
  69. Car::display();
  70. cout << "Model Type: " << modelType << endl;
  71. }
  72. };
  73.  
  74. // Subclass Ford of Car
  75. class Ford : public Car {
  76. string modelType;
  77.  
  78. public:
  79. Ford(double m, double p, double oc, int w, int sc, const string& ft, const string& mt)
  80. : Car(m, p, oc, w, sc, ft), modelType(mt) {}
  81.  
  82. void display() const override {
  83. Car::display();
  84. cout << "Model Type: " << modelType << endl;
  85. }
  86. };
  87.  
  88. // Subclass Bajaj of Bike
  89. class Bajaj : public Bike {
  90. string makeType;
  91.  
  92. public:
  93. Bajaj(double m, double p, int nc, int ng, const string& ct, const string& wt, double fts, const string& mt)
  94. : Bike(m, p, nc, ng, ct, wt, fts), makeType(mt) {}
  95.  
  96. void display() const override {
  97. Bike::display();
  98. cout << "Make Type: " << makeType << endl;
  99. }
  100. };
  101.  
  102. // Subclass TVS of Bike
  103. class TVS : public Bike {
  104. string makeType;
  105.  
  106. public:
  107. TVS(double m, double p, int nc, int ng, const string& ct, const string& wt, double fts, const string& mt)
  108. : Bike(m, p, nc, ng, ct, wt, fts), makeType(mt) {}
  109.  
  110. void display() const override {
  111. Bike::display();
  112. cout << "Make Type: " << makeType << endl;
  113. }
  114. };
  115.  
  116. int main() {
  117. // Create instances of Audi and Ford
  118. Audi myAudi(15.0, 50000, 2000, 5, 5, "Petrol", "Audi A4");
  119. Ford myFord(12.0, 30000, 1500, 3, 5, "Diesel", "Ford Mustang");
  120.  
  121. // Create instances of Bajaj and TVS
  122. Bajaj myBajaj(50.0, 1500, 1, 5, "Air", "Alloys", 13.0, "Bajaj Pulsar");
  123. TVS myTVS(55.0, 1200, 1, 4, "Oil", "Spokes", 12.5, "TVS Apache");
  124.  
  125. // Display details
  126. cout << "Audi Car Details:" << endl;
  127. myAudi.display();
  128. cout << endl;
  129.  
  130. cout << "Ford Car Details:" << endl;
  131. myFord.display();
  132. cout << endl;
  133.  
  134. cout << "Bajaj Bike Details:" << endl;
  135. myBajaj.display();
  136. cout << endl;
  137.  
  138. cout << "TVS Bike Details:" << endl;
  139. myTVS.display();
  140.  
  141. return 0;
  142. }
  143.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Audi Car Details:
Mileage: 15 km/l, Price: $50000
Ownership Cost: $2000, Warranty: 5 years, Seating Capacity: 5, Fuel Type: Petrol
Model Type: Audi A4

Ford Car Details:
Mileage: 12 km/l, Price: $30000
Ownership Cost: $1500, Warranty: 3 years, Seating Capacity: 5, Fuel Type: Diesel
Model Type: Ford Mustang

Bajaj Bike Details:
Mileage: 50 km/l, Price: $1500
Number of Cylinders: 1, Number of Gears: 5, Cooling Type: Air, Wheel Type: Alloys, Fuel Tank Size: 13 inches
Make Type: Bajaj Pulsar

TVS Bike Details:
Mileage: 55 km/l, Price: $1200
Number of Cylinders: 1, Number of Gears: 4, Cooling Type: Oil, Wheel Type: Spokes, Fuel Tank Size: 12.5 inches
Make Type: TVS Apache