Advertisement
rajeshinternshala

Untitled

Jan 27th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Car {
  7. public:
  8.     int license;
  9.     string brand;
  10.     virtual int MAXSeats() = 0;
  11. };
  12.  
  13. class SUV : public Car {
  14. public:
  15.     int seats;
  16.     int extra_seats;
  17.  
  18.     SUV() {
  19.         extra_seats = 1;
  20.     }
  21.  
  22.     int MAXSeats() override {
  23.         return (seats + extra_seats);
  24.     }
  25. };
  26.  
  27. class Sedan : public Car {
  28. public:
  29.     int seats;
  30.     bool sport_package;
  31.  
  32.     Sedan() {
  33.         sport_package = false;
  34.     }
  35.  
  36.     int MAXSeats() override {
  37.         return (seats + (sport_package ? 1 : 0));
  38.     }
  39. };
  40.  
  41. class CarRental {
  42. private:
  43.     vector<Car*> cars;
  44.  
  45. public:
  46.     void addCar(Car* car) {
  47.         cars.push_back(car);
  48.     }
  49.  
  50.     int getCarSeats(int license) {
  51.         for (Car* car : cars) {
  52.             if (car->license == license) {
  53.                 return car->MAXSeats();
  54.             }
  55.         }
  56.         return -1; // License not found
  57.     }
  58. };
  59.  
  60. int main() {
  61.     CarRental rental;
  62.  
  63.     SUV suv1;
  64.     suv1.license = 1001;
  65.     suv1.brand = "BMW";
  66.     suv1.seats = 5;
  67.  
  68.     Sedan sedan1;
  69.     sedan1.license = 2001;
  70.     sedan1.brand = "Toyota";
  71.     sedan1.seats = 4;
  72.     sedan1.sport_package = true;
  73.  
  74.     rental.addCar(&suv1);
  75.     rental.addCar(&sedan1);
  76.  
  77.     int licenseToQuery = 1001;
  78.     int maxSeats = rental.getCarSeats(licenseToQuery);
  79.  
  80.     if (maxSeats != -1) {
  81.         cout << "License: " << licenseToQuery << " can carry a maximum of " << maxSeats << " passengers." << endl;
  82.     } else {
  83.         cout << "License not found." << endl;
  84.     }
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement