diff options
Diffstat (limited to 'week12')
| -rw-r--r-- | week12/Exercise3/MobileDevice.h | 1 | ||||
| -rw-r--r-- | week12/Exercise3/Pager.cpp | 4 | ||||
| -rw-r--r-- | week12/Exercise3/Pager.h | 1 | ||||
| -rw-r--r-- | week12/Exercise3/TelecommunicationCompany.cpp | 22 | ||||
| -rw-r--r-- | week12/Exercise3/TelecommunicationCompany.h | 3 | ||||
| -rw-r--r-- | week12/Exercise3/Telephone.cpp | 4 | ||||
| -rw-r--r-- | week12/Exercise3/Telephone.h | 1 |
7 files changed, 36 insertions, 0 deletions
diff --git a/week12/Exercise3/MobileDevice.h b/week12/Exercise3/MobileDevice.h index b9de538..958eb86 100644 --- a/week12/Exercise3/MobileDevice.h +++ b/week12/Exercise3/MobileDevice.h @@ -9,6 +9,7 @@ public: virtual ~MobileDevice() = default; virtual void Show() = 0; + virtual MobileDevice* clone() = 0; bool CanAccept(const char* hyperMessage); void Accept(const char* hyperMessage); }; diff --git a/week12/Exercise3/Pager.cpp b/week12/Exercise3/Pager.cpp index 18beb4b..acd0684 100644 --- a/week12/Exercise3/Pager.cpp +++ b/week12/Exercise3/Pager.cpp @@ -47,6 +47,10 @@ Pager& Pager::operator=(Pager&& other) { return *this; } +MobileDevice* Pager::clone() { + return new Pager(*this); +} + void Pager::Show() { std::ofstream outFile(fileName); if (!outFile.is_open()) { diff --git a/week12/Exercise3/Pager.h b/week12/Exercise3/Pager.h index 451112e..8115d64 100644 --- a/week12/Exercise3/Pager.h +++ b/week12/Exercise3/Pager.h @@ -15,5 +15,6 @@ public: Pager(Pager&& other); Pager& operator=(Pager&& other); + virtual MobileDevice* clone() override; virtual void Show() override; }; diff --git a/week12/Exercise3/TelecommunicationCompany.cpp b/week12/Exercise3/TelecommunicationCompany.cpp index 7b877ca..34987a4 100644 --- a/week12/Exercise3/TelecommunicationCompany.cpp +++ b/week12/Exercise3/TelecommunicationCompany.cpp @@ -1,5 +1,6 @@ #include "TelecommunicationCompany.h" #include "MobileDevice.h" +#include "Telephone.h" void TelecommunicationCompany::resize() { allocated *= 2; @@ -18,6 +19,15 @@ void TelecommunicationCompany::free() { delete[] devices; } +void TelecommunicationCompany::copyFrom(const TelecommunicationCompany& other) { + this->allocated = other.allocated; + this->size = other.size; + this->devices = new MobileDevice*[size]; + for (int i = 0; i < size; i++) { + this->devices[i] = other.devices[i]->clone(); + } +} + TelecommunicationCompany::TelecommunicationCompany() { devices = nullptr; allocated = size = 0; @@ -27,6 +37,18 @@ TelecommunicationCompany::~TelecommunicationCompany() { free(); } +TelecommunicationCompany::TelecommunicationCompany(const TelecommunicationCompany& other) { + copyFrom(other); +} + +TelecommunicationCompany& TelecommunicationCompany::operator=(const TelecommunicationCompany& other) { + if (this != &other) { + copyFrom(other); + free(); + } + return *this; +} + TelecommunicationCompany::TelecommunicationCompany(TelecommunicationCompany&& other) { this->size = other.size; this->allocated = other.allocated; diff --git a/week12/Exercise3/TelecommunicationCompany.h b/week12/Exercise3/TelecommunicationCompany.h index 22ce731..405d134 100644 --- a/week12/Exercise3/TelecommunicationCompany.h +++ b/week12/Exercise3/TelecommunicationCompany.h @@ -9,10 +9,13 @@ class TelecommunicationCompany { void resize(); void free(); + void copyFrom(const TelecommunicationCompany& other); public: TelecommunicationCompany(); ~TelecommunicationCompany(); + TelecommunicationCompany(const TelecommunicationCompany& other); + TelecommunicationCompany& operator=(const TelecommunicationCompany& other); TelecommunicationCompany(TelecommunicationCompany&& other); TelecommunicationCompany& operator=(TelecommunicationCompany&& other); }; diff --git a/week12/Exercise3/Telephone.cpp b/week12/Exercise3/Telephone.cpp index 1250616..1037032 100644 --- a/week12/Exercise3/Telephone.cpp +++ b/week12/Exercise3/Telephone.cpp @@ -1,6 +1,10 @@ #include "Telephone.h" #include <iostream> +MobileDevice* Telephone::clone() { + return new Telephone(*this); +} + void Telephone::Show() { std::cout << textMessage << std::endl; } diff --git a/week12/Exercise3/Telephone.h b/week12/Exercise3/Telephone.h index aac94e6..0277ac3 100644 --- a/week12/Exercise3/Telephone.h +++ b/week12/Exercise3/Telephone.h @@ -2,5 +2,6 @@ #include "MobileDevice.h" class Telephone : public MobileDevice { + virtual MobileDevice* clone() override; virtual void Show() override; }; |
