Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Car {
- public:
- int license;
- string brand;
- virtual int MAXSeats() = 0;
- };
- class SUV : public Car {
- public:
- int seats;
- int extra_seats;
- SUV() {
- extra_seats = 1;
- }
- int MAXSeats() override {
- return (seats + extra_seats);
- }
- };
- class Sedan : public Car {
- public:
- int seats;
- bool sport_package;
- Sedan() {
- sport_package = false;
- }
- int MAXSeats() override {
- return (seats + (sport_package ? 1 : 0));
- }
- };
- class CarRental {
- private:
- vector<Car*> cars;
- public:
- void addCar(Car* car) {
- cars.push_back(car);
- }
- int getCarSeats(int license) {
- for (Car* car : cars) {
- if (car->license == license) {
- return car->MAXSeats();
- }
- }
- return -1; // License not found
- }
- };
- int main() {
- CarRental rental;
- SUV suv1;
- suv1.license = 1001;
- suv1.brand = "BMW";
- suv1.seats = 5;
- Sedan sedan1;
- sedan1.license = 2001;
- sedan1.brand = "Toyota";
- sedan1.seats = 4;
- sedan1.sport_package = true;
- rental.addCar(&suv1);
- rental.addCar(&sedan1);
- int licenseToQuery = 1001;
- int maxSeats = rental.getCarSeats(licenseToQuery);
- if (maxSeats != -1) {
- cout << "License: " << licenseToQuery << " can carry a maximum of " << maxSeats << " passengers." << endl;
- } else {
- cout << "License not found." << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement