Created
March 23, 2026 10:49
-
-
Save Vitka999/01f597727130b54b1018a69134993cd5 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 <string> | |
| using namespace std; | |
| class Cloneable { | |
| public: | |
| virtual ~Cloneable() = default; | |
| virtual Cloneable* clone() const = 0; | |
| }; | |
| class Engine : public Cloneable { | |
| public: | |
| string type; | |
| int power; | |
| Engine(string t, int p) : type(t), power(p) {} | |
| Engine* clone() const override { return new Engine(*this); } | |
| void show() const { cout << " Engine: " << type << ", " << power << " hp\n"; } | |
| }; | |
| class Transmission : public Cloneable { | |
| public: | |
| string kind; | |
| int gears; | |
| Transmission(string k, int g) : kind(k), gears(g) {} | |
| Transmission* clone() const override { return new Transmission(*this); } | |
| void show() const { cout << " Transmission: " << kind << ", " << gears << " gears\n"; } | |
| }; | |
| class Manufacturer : public Cloneable { | |
| public: | |
| string name; | |
| string country; | |
| Manufacturer(string n, string c) : name(n), country(c) {} | |
| Manufacturer* clone() const override { return new Manufacturer(*this); } | |
| void show() const { cout << " Manufacturer: " << name << ", " << country << "\n"; } | |
| }; | |
| class Wheel : public Cloneable { | |
| public: | |
| int size; | |
| string season; | |
| Wheel(int s, string sn) : size(s), season(sn) {} | |
| Wheel* clone() const override { return new Wheel(*this); } | |
| void show() const { cout << " Wheel: " << size << " inch, " << season << "\n"; } | |
| }; | |
| class Interior : public Cloneable { | |
| public: | |
| string material; | |
| bool heated; | |
| Interior(string m, bool h) : material(m), heated(h) {} | |
| Interior* clone() const override { return new Interior(*this); } | |
| void show() const { | |
| cout << " Interior: " << material << (heated ? ", heated seats\n" : ", no heating\n"); | |
| } | |
| }; | |
| class Car : public Cloneable { | |
| public: | |
| string model; | |
| string color; | |
| Engine* engine; | |
| Transmission* transmission; | |
| Manufacturer* manufacturer; | |
| Wheel* wheel; | |
| Interior* interior; | |
| Car(string m, string c, | |
| Engine* e, Transmission* t, Manufacturer* man, Wheel* w, Interior* i) | |
| : model(m), color(c), engine(e), transmission(t), manufacturer(man), wheel(w), interior(i) {} | |
| Car(const Car& other) | |
| : model(other.model), color(other.color), | |
| engine(other.engine ? other.engine->clone() : nullptr), | |
| transmission(other.transmission ? other.transmission->clone() : nullptr), | |
| manufacturer(other.manufacturer ? other.manufacturer->clone() : nullptr), | |
| wheel(other.wheel ? other.wheel->clone() : nullptr), | |
| interior(other.interior ? other.interior->clone() : nullptr) {} | |
| ~Car() { | |
| delete engine; | |
| delete transmission; | |
| delete manufacturer; | |
| delete wheel; | |
| delete interior; | |
| } | |
| Car* clone() const override { | |
| return new Car(*this); | |
| } | |
| void show() const { | |
| cout << "Car: " << model << ", color " << color << "\n"; | |
| if (engine) engine->show(); | |
| if (transmission) transmission->show(); | |
| if (manufacturer) manufacturer->show(); | |
| if (wheel) wheel->show(); | |
| if (interior) interior->show(); | |
| cout << "--------------------\n"; | |
| } | |
| }; | |
| int main() { | |
| Car* prototype = new Car("Camry", "perple", | |
| new Engine("gasoline", 150), | |
| new Transmission("manual", 6), | |
| new Manufacturer("Toyota", "Japan"), | |
| new Wheel(17, "summer"), | |
| new Interior("fabric", false)); | |
| cout << "ORIGINAL\n"; | |
| prototype->show(); | |
| Car* clone = prototype->clone(); | |
| clone->model = "Camry Sport"; | |
| clone->color = "pink"; | |
| clone->engine->type = "gasoline + turbo"; | |
| clone->engine->power = 200; | |
| clone->transmission->kind = "automatic"; | |
| clone->transmission->gears = 8; | |
| clone->interior->material = "leather"; | |
| clone->interior->heated = true; | |
| cout << "CLONE\n"; | |
| clone->show(); | |
| cout << "ORIGINAL UNCHANGED\n"; | |
| prototype->show(); | |
| delete prototype; | |
| delete clone; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment