Created
March 18, 2026 11:53
-
-
Save vlaleli/579ef9c5c97e7e70dfd54d04067c1f2a 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 Engine { | |
| private: | |
| string type; | |
| public: | |
| Engine(string t = "Petrol") : type(t) {} | |
| string getType() const { return type; } | |
| }; | |
| class Gearbox { | |
| private: | |
| string type; | |
| public: | |
| Gearbox(string t = "Manual") : type(t) {} | |
| string getType() const { return type; } | |
| }; | |
| class Manufacturer { | |
| private: | |
| string name; | |
| public: | |
| Manufacturer(string n = "Unknown") : name(n) {} | |
| string getName() const { return name; } | |
| }; | |
| class Wheels { | |
| private: | |
| int count; | |
| public: | |
| Wheels(int c = 4) : count(c) {} | |
| int getCount() const { return count; } | |
| }; | |
| class Color { | |
| private: | |
| string value; | |
| public: | |
| Color(string v = "White") : value(v) {} | |
| string getValue() const { return value; } | |
| void setValue(string v) { value = v; } | |
| }; | |
| class Car { | |
| private: | |
| Engine engine; | |
| Gearbox gearbox; | |
| Manufacturer manufacturer; | |
| Wheels wheels; | |
| Color color; | |
| public: | |
| Car(Engine e, Gearbox g, Manufacturer m, Wheels w, Color c) | |
| : engine(e), gearbox(g), manufacturer(m), wheels(w), color(c) {} | |
| Car* clone() const { | |
| return new Car(*this); | |
| } | |
| void setColor(string c) { | |
| color.setValue(c); | |
| } | |
| void show() const { | |
| cout << "Engine: " << engine.getType() << endl; | |
| cout << "Gearbox: " << gearbox.getType() << endl; | |
| cout << "Manufacturer: " << manufacturer.getName() << endl; | |
| cout << "Wheels: " << wheels.getCount() << endl; | |
| cout << "Color: " << color.getValue() << endl; | |
| } | |
| }; | |
| int main() { | |
| Car car1( | |
| Engine("Diesel"), | |
| Gearbox("Automatic"), | |
| Manufacturer("BMW"), | |
| Wheels(4), | |
| Color("Black") | |
| ); | |
| cout << "Original car:" << endl; | |
| car1.show(); | |
| cout << endl; | |
| Car* car2 = car1.clone(); | |
| car2->setColor("Red"); | |
| cout << "Cloned car:" << endl; | |
| car2->show(); | |
| delete car2; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment