Created
May 5, 2026 07:15
-
-
Save m1irka/6b2a26d009486039a52fa53b5a99f6d6 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> | |
| #include <fstream> | |
| #include <string> | |
| #include <vector> | |
| #include <algorithm> | |
| using namespace std; | |
| class House; | |
| class Bank; | |
| class Factory; | |
| class IVisitor abstract | |
| { | |
| public: | |
| virtual void VisitHouse(House* h) = 0; | |
| virtual void VisitBank(Bank* b) = 0; | |
| virtual void VisitFactory(Factory* f) = 0; | |
| }; | |
| class AbstractBuilding abstract | |
| { | |
| string Owner; | |
| public: | |
| virtual void Accept(IVisitor* visitor)abstract; | |
| string GetOwner() | |
| { | |
| return Owner; | |
| } | |
| void SetOwner(string o) | |
| { | |
| this->Owner = o; | |
| } | |
| }; | |
| class House : public AbstractBuilding | |
| { | |
| public: | |
| void Accept(IVisitor* visitor) override | |
| { | |
| visitor->VisitHouse(this); | |
| } | |
| }; | |
| class Factory : public AbstractBuilding | |
| { | |
| public: | |
| void Accept(IVisitor* visitor) override | |
| { | |
| visitor->VisitFactory(this); | |
| } | |
| }; | |
| class Bank : public AbstractBuilding | |
| { | |
| public: | |
| void Accept(IVisitor* visitor) override | |
| { | |
| visitor->VisitBank(this); | |
| } | |
| }; | |
| class InsuranceAgent : public IVisitor | |
| { | |
| public: | |
| void VisitHouse(House* h) override | |
| { | |
| cout << "Offering medical insurance House owner: " << h->GetOwner() << endl; | |
| } | |
| void VisitBank(Bank* b) override | |
| { | |
| cout << "Offering robbery insurance Bank owner: " << b->GetOwner() << endl; | |
| } | |
| void VisitFactory(Factory* f) override | |
| { | |
| cout << "Offering fire or flood insurance Factory owner: " << f->GetOwner() << endl; | |
| } | |
| }; | |
| class City | |
| { | |
| vector<AbstractBuilding*> buildings; | |
| public: | |
| void Add(AbstractBuilding* b) {buildings.push_back(b);} | |
| void Accept(IVisitor* visitor) | |
| { | |
| for (auto b : buildings) b->Accept(visitor); | |
| } | |
| }; | |
| int main() | |
| { | |
| City city; | |
| House* h = new House; | |
| h->SetOwner("Yaroslav Posinko"); | |
| Bank* b = new Bank; | |
| b->SetOwner("Privat Bank"); | |
| Factory* f = new Factory; | |
| f->SetOwner("Steel wokres"); | |
| city.Add(h); | |
| city.Add(b); | |
| city.Add(f); | |
| InsuranceAgent agent; | |
| city.Accept(&agent); | |
| delete h; | |
| delete b; | |
| delete f; | |
| system("pause"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment