1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #include <iostream>
class Address { public: std::string street, city; int suite; Address(const std::string& street, const std::string& city, const int suite) : street{street}, city{city}, suite{suite} {}
virtual Address* clone() { return new Address{street, city, suite}; } };
class Contact { public: std::string name; Address* address; Contact(const std::string name, Address* address) : name{name} , address{address} {} Contact(const Contact& other) : name{other.name} , address{ new Address{*other.address} } {}
Contact& operator=(const Contact& other) { if (this == &other) return *this; name = other.name; address = other.address; return *this; } };
class ExtendedAddress : public Address { public: std::string country, postcode; ExtendedAddress(const std::string &street, const std::string &city, const int suite, const std::string &country, const std::string &postcode) : Address(street, city, suite) , country{country}, postcode{postcode} {}
ExtendedAddress* clone() override { return new ExtendedAddress(street, city, suite, country, postcode); } };
int main(void) { ExtendedAddress ea{"123 West Dr", "London", 123, "UK", "SW101EG"}; Address& a = ea; auto cloned = a.clone();
return 0; }
|