Created
May 4, 2026 10:12
-
-
Save XoLinA/39a05593220805caf133f9ee797678da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| class House; | |
| class Bank; | |
| class Factory; | |
| class InsuranceAgent { | |
| public: | |
| virtual void visit(House* house) = 0; | |
| virtual void visit(Bank* bank) = 0; | |
| virtual void visit(Factory* factory) = 0; | |
| }; | |
| class Building { | |
| public: | |
| virtual void accept(InsuranceAgent* agent) = 0; | |
| virtual ~Building() {} | |
| }; | |
| class House : public Building { | |
| public: | |
| void accept(InsuranceAgent* agent) override | |
| { | |
| agent->visit(this); | |
| } | |
| }; | |
| class Bank : public Building { | |
| public: | |
| void accept(InsuranceAgent* agent) override | |
| { | |
| agent->visit(this); | |
| } | |
| }; | |
| class Factory : public Building { | |
| public: | |
| void accept(InsuranceAgent* agent) override | |
| { | |
| agent->visit(this); | |
| } | |
| }; | |
| class BeginnerAgent : public InsuranceAgent { | |
| public: | |
| void visit(House* house) override | |
| { | |
| cout << "Home: Offer health insurance for the family.\n"; | |
| } | |
| void visit(Bank* bank) override | |
| { | |
| cout << "Bank: Offer robbery insurance.\n"; | |
| } | |
| void visit(Factory* factory) override | |
| { | |
| cout << "Factory: Offer fire and flood insurance.\n"; | |
| } | |
| }; | |
| int main() { | |
| Building* buildings[] = {new House(),new Bank(), new Factory()}; | |
| InsuranceAgent* agent = new BeginnerAgent(); | |
| for (Building* b : buildings) | |
| { | |
| b->accept(agent); | |
| } | |
| for (Building* b : buildings) | |
| { | |
| delete b; | |
| } | |
| delete agent; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment