Created
April 30, 2026 11:52
-
-
Save vlaleli/df5c3560ee260b8bfce1f8d2e797d5c7 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 <vector> | |
| #include <memory> | |
| #include <string> | |
| using namespace std; | |
| class FamilyHouse; | |
| class Bank; | |
| class Factory; | |
| class Visitor { | |
| public: | |
| virtual void visitFamilyHouse(FamilyHouse& house) = 0; | |
| virtual void visitBank(Bank& bank) = 0; | |
| virtual void visitFactory(Factory& factory) = 0; | |
| virtual ~Visitor() = default; | |
| }; | |
| class Building { | |
| protected: | |
| string address; | |
| public: | |
| Building(const string& address) : address(address) {} | |
| string getAddress() const { | |
| return address; | |
| } | |
| virtual void accept(Visitor& visitor) = 0; | |
| virtual ~Building() = default; | |
| }; | |
| class FamilyHouse : public Building { | |
| public: | |
| FamilyHouse(const string& address) : Building(address) {} | |
| void accept(Visitor& visitor) override { | |
| visitor.visitFamilyHouse(*this); | |
| } | |
| }; | |
| class Bank : public Building { | |
| public: | |
| Bank(const string& address) : Building(address) {} | |
| void accept(Visitor& visitor) override { | |
| visitor.visitBank(*this); | |
| } | |
| }; | |
| class Factory : public Building { | |
| public: | |
| Factory(const string& address) : Building(address) {} | |
| void accept(Visitor& visitor) override { | |
| visitor.visitFactory(*this); | |
| } | |
| }; | |
| class InsuranceAgent : public Visitor { | |
| private: | |
| string name; | |
| public: | |
| InsuranceAgent(const string& name) : name(name) {} | |
| void visitFamilyHouse(FamilyHouse& house) override { | |
| cout << "Агент " << name << " відвідав будинок: " << house.getAddress() << endl; | |
| cout << "-> Пропозиція: медична страховка для сім'ї\n\n"; | |
| } | |
| void visitBank(Bank& bank) override { | |
| cout << "Агент " << name << " відвідав банк: " << bank.getAddress() << endl; | |
| cout << "-> Пропозиція: страховка на випадок пограбування\n\n"; | |
| } | |
| void visitFactory(Factory& factory) override { | |
| cout << "Агент " << name << " відвідав фабрику: " << factory.getAddress() << endl; | |
| cout << "-> Пропозиція: страхування підприємства (пожежа/повінь)\n\n"; | |
| } | |
| }; | |
| class AuditAgent : public Visitor { | |
| public: | |
| void visitFamilyHouse(FamilyHouse& house) override { | |
| cout << "[АУДИТ] Перевірка будинку: " << house.getAddress() << endl; | |
| } | |
| void visitBank(Bank& bank) override { | |
| cout << "[АУДИТ] Перевірка банку: " << bank.getAddress() << endl; | |
| } | |
| void visitFactory(Factory& factory) override { | |
| cout << "[АУДИТ] Перевірка фабрики: " << factory.getAddress() << endl; | |
| } | |
| }; | |
| int main() { | |
| vector<unique_ptr<Building>> buildings; | |
| buildings.push_back(make_unique<FamilyHouse>("вул. Сімейна, 12")); | |
| buildings.push_back(make_unique<Bank>("просп. Фінансовий, 5")); | |
| buildings.push_back(make_unique<Factory>("вул. Промислова, 21")); | |
| buildings.push_back(make_unique<FamilyHouse>("вул. Затишна, 8")); | |
| buildings.push_back(make_unique<Bank>("вул. Центральна, 1")); | |
| InsuranceAgent agent("Іван"); | |
| AuditAgent audit; | |
| cout << "=== СТРАХОВИЙ АГЕНТ ===\n\n"; | |
| for (auto& building : buildings) { | |
| building->accept(agent); | |
| } | |
| cout << "=== АУДИТ ===\n\n"; | |
| for (auto& building : buildings) { | |
| building->accept(audit); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment