fork download
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class Vehicle {
  6. protected:
  7. string brand;
  8. public:
  9. Vehicle() {}
  10. Vehicle(string b) {
  11. brand = b;
  12. }
  13. Vehicle(const Vehicle& other) {
  14. brand = other.brand;
  15. }
  16. ~Vehicle() {}
  17. };
  18.  
  19. class Car : public Vehicle {
  20. protected:
  21. int seats;
  22. public:
  23. Car() {}
  24. Car(string b, int s) : Vehicle(b) {
  25. seats = s;
  26. }
  27. Car(const Car& other) : Vehicle(other) {
  28. seats = other.seats;
  29. }
  30. ~Car() {}
  31. };
  32.  
  33. class SportsCar : public Car {
  34. private:
  35. float topSpeed;
  36. public:
  37. SportsCar() {}
  38. SportsCar(string b, int s, float ts) : Car(b, s) {
  39. topSpeed = ts;
  40. }
  41.  
  42. float getSpeed() const {
  43. return topSpeed;
  44. }
  45.  
  46. string getBrand() const {
  47. return brand;
  48. }
  49. ~SportsCar() {}
  50. };
  51.  
  52. // Function to compare speeds and print the fastest car
  53. void compare(SportsCar cars[], int n) {
  54. int maxIndex = 0;
  55. for(int i = 1; i < n; i++) {
  56. if(cars[i].getSpeed() > cars[maxIndex].getSpeed()) {
  57. maxIndex = i;
  58. }
  59. }
  60.  
  61. cout << "The fastest car is: " << cars[maxIndex].getBrand()
  62. << " with speed " << cars[maxIndex].getSpeed() << " m/s" << endl;
  63. }
  64.  
  65. int main() {
  66. int n;
  67. cout << "Enter number of Sports Cars: ";
  68. cin >> n;
  69.  
  70. SportsCar cars[10];
  71. string name;
  72. float speed;
  73.  
  74. for(int i = 0; i < n; i++) {
  75. cout << "Enter brand of car " << i+1 << ": ";
  76. cin >> name;
  77. cout << "Enter speed of car " << i+1 << " (in m/s): ";
  78. cin >> speed;
  79. cars[i] = SportsCar(name, 2, speed); // assuming 2 seats for all
  80. }
  81.  
  82. for(int i = 0; i < n; i++) {
  83. cout << "Car " << i+1 << ": " << cars[i].getBrand()
  84. << " with speed " << cars[i].getSpeed() << " m/s" << endl;
  85. }
  86.  
  87. compare(cars, n);
  88.  
  89. return 0;
  90. }
  91.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Enter number of Sports Cars: The fastest car is:  with speed 0 m/s