Last active
March 4, 2026 12:02
-
-
Save vlaleli/7bb9856639da56bb676d0a4694fe68cb 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 <memory> | |
| #include <string> | |
| using namespace std; | |
| class Transport { | |
| public: | |
| virtual ~Transport() = default; | |
| virtual void input() = 0; | |
| virtual void show() = 0; | |
| virtual void deliver() = 0; | |
| }; | |
| class Truck : public Transport { | |
| private: | |
| string number; | |
| double capacity; | |
| int wheels; | |
| public: | |
| void input() override { | |
| cout << "Truck number: "; | |
| cin >> number; | |
| cout << "Capacity: "; | |
| cin >> capacity; | |
| cout << "Wheels: "; | |
| cin >> wheels; | |
| } | |
| void show() override { | |
| cout << "Truck | Number: " << number << " Capacity: " << capacity << " Wheels: " << wheels << endl; | |
| } | |
| void deliver() override { | |
| cout << "Delivery by road" << endl; | |
| } | |
| }; | |
| class Ship : public Transport { | |
| private: | |
| string name; | |
| string port; | |
| double tonnage; | |
| public: | |
| void input() override { | |
| cout << "Ship name: "; | |
| cin >> name; | |
| cout << "Port: "; | |
| cin >> port; | |
| cout << "Tonnage: "; | |
| cin >> tonnage; | |
| } | |
| void show() override { | |
| cout << "Ship | Name: " << name << " Port: " << port << " Tonnage: " << tonnage << endl; | |
| } | |
| void deliver() override { | |
| cout << "Delivery by sea" << endl; | |
| } | |
| }; | |
| class Plane : public Transport { | |
| private: | |
| string number; | |
| int engines; | |
| double range; | |
| public: | |
| void input() override { | |
| cout << "Plane number: "; | |
| cin >> number; | |
| cout << "Engines: "; | |
| cin >> engines; | |
| cout << "Range: "; | |
| cin >> range; | |
| } | |
| void show() override { | |
| cout << "Plane | Number: " << number << " Engines: " << engines << " Range: " << range << endl; | |
| } | |
| void deliver() override { | |
| cout << "Delivery by air" << endl; | |
| } | |
| }; | |
| class Logistics { | |
| public: | |
| virtual ~Logistics() = default; | |
| virtual unique_ptr<Transport> createTransport() = 0; | |
| void planDelivery() { | |
| auto transport = createTransport(); | |
| transport->input(); | |
| transport->show(); | |
| transport->deliver(); | |
| } | |
| }; | |
| class RoadLogistics : public Logistics { | |
| public: | |
| unique_ptr<Transport> createTransport() override { | |
| return make_unique<Truck>(); | |
| } | |
| }; | |
| class SeaLogistics : public Logistics { | |
| public: | |
| unique_ptr<Transport> createTransport() override { | |
| return make_unique<Ship>(); | |
| } | |
| }; | |
| class AirLogistics : public Logistics { | |
| public: | |
| unique_ptr<Transport> createTransport() override { | |
| return make_unique<Plane>(); | |
| } | |
| }; | |
| int main() { | |
| int choice; | |
| cout << "1 Truck\n2 Ship\n3 Plane\nChoice: "; | |
| cin >> choice; | |
| unique_ptr<Logistics> logistics; | |
| if (choice == 1) logistics = make_unique<RoadLogistics>(); | |
| else if (choice == 2) logistics = make_unique<SeaLogistics>(); | |
| else logistics = make_unique<AirLogistics>(); | |
| logistics->planDelivery(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment