fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. class Car
  7. {
  8. private:
  9. string plateNumber;
  10. string model;
  11. double dailyRate;
  12. int daysRented;
  13.  
  14. public:
  15. // ---------------- Static Members ----------------
  16. static int activeCars;
  17. static const int MAX_DAYS = 30;
  18.  
  19. // ---------------- Constructors and Destructor ----------------
  20.  
  21. // Default constructor
  22. Car()
  23. {
  24. plateNumber = "NONE";
  25. model = "UNKNOWN";
  26. dailyRate = 0.0;
  27. daysRented = 0;
  28.  
  29. activeCars++;
  30. }
  31.  
  32. // Parameterized constructor
  33. Car(string plate, string mod, double rate, int days)
  34. {
  35. plateNumber = plate;
  36. model = mod;
  37. dailyRate = rate;
  38.  
  39. if (days > MAX_DAYS)
  40. daysRented = MAX_DAYS;
  41. else
  42. daysRented = days;
  43.  
  44. activeCars++;
  45. }
  46.  
  47. // Destructor
  48. ~Car()
  49. {
  50. cout << "Car " << plateNumber << " removed from system." << endl;
  51. activeCars--;
  52. }
  53.  
  54. // ---------------- Operator Overloading ----------------
  55. bool operator==(const Car& other) const
  56. {
  57. return plateNumber == other.plateNumber;
  58. }
  59.  
  60. // ---------------- Member Functions ----------------
  61. void Print() const
  62. {
  63. cout << fixed << setprecision(2);
  64. cout << "Plate: " << plateNumber
  65. << " | Model: " << model
  66. << " | Rate: $" << dailyRate
  67. << " | Days Rented: " << daysRented << endl;
  68. }
  69.  
  70. static int GetActiveCars()
  71. {
  72. return activeCars;
  73. }
  74.  
  75. string GetPlate() const
  76. {
  77. return plateNumber;
  78. }
  79. };
  80.  
  81. // Initialize static variable
  82. int Car::activeCars = 0;
  83.  
  84.  
  85. int main()
  86. {
  87. cout << "\n********* Creating an array of Car objects *********" << endl;
  88.  
  89. // Array of 5 Car objects
  90. Car cars[5] = {
  91. Car("TX123", "Toyota Corolla", 45.99, 10),
  92. Car("AR777", "Honda Civic", 40.50, 5),
  93. Car("OK999", "Ford Focus", 38.25, 31), // capped at MAX_DAYS
  94. Car("MO555", "Nissan Altima", 50.00, 20),
  95. Car("TX123", "Toyota Camry", 55.00, 7) // same plate as first
  96. };
  97.  
  98. cout << "\nActive cars: " << Car::GetActiveCars() << endl;
  99.  
  100. cout << "\n********* Displaying all cars *********" << endl;
  101. for (int i = 0; i < 5; i++)
  102. cars[i].Print();
  103.  
  104. cout << "\n********* Operator Overloading *********" << endl;
  105. if (cars[0] == cars[4])
  106. cout << "Cars " << cars[0].GetPlate() << " and " << cars[4].GetPlate()
  107. << " are the SAME vehicle." << endl;
  108. else
  109. cout << "Cars have different plate numbers." << endl;
  110.  
  111. cout << "\nActive cars: " << Car::GetActiveCars() << endl;
  112.  
  113. cout << "********* End of program *********" << endl;
  114.  
  115. return 0;
  116. }
  117.  
  118.  
  119.  
Success #stdin #stdout 0s 5324KB
stdin
45
stdout
********* Creating an array of Car objects *********

Active cars: 5

********* Displaying all cars *********
Plate: TX123 | Model: Toyota Corolla | Rate: $45.99 | Days Rented: 10
Plate: AR777 | Model: Honda Civic | Rate: $40.50 | Days Rented: 5
Plate: OK999 | Model: Ford Focus | Rate: $38.25 | Days Rented: 30
Plate: MO555 | Model: Nissan Altima | Rate: $50.00 | Days Rented: 20
Plate: TX123 | Model: Toyota Camry | Rate: $55.00 | Days Rented: 7

********* Operator Overloading *********
Cars TX123 and TX123 are the SAME vehicle.

Active cars: 5
********* End of program *********
Car TX123 removed from system.
Car MO555 removed from system.
Car OK999 removed from system.
Car AR777 removed from system.
Car TX123 removed from system.