diff options
| -rw-r--r-- | week12/Exercise1/Publication.cpp | 5 | ||||
| -rw-r--r-- | week12/Exercise1/Publication.h | 11 | ||||
| -rw-r--r-- | week12/Exercise1/ScientificPaper.cpp | 5 | ||||
| -rw-r--r-- | week12/Exercise1/ScientificPaper.h | 12 | ||||
| -rw-r--r-- | week12/Exercise1/TextDocument.cpp | 53 | ||||
| -rw-r--r-- | week12/Exercise1/TextDocument.h | 20 | ||||
| -rw-r--r-- | week12/Exercise3/MobileDevice.cpp | 15 | ||||
| -rw-r--r-- | week12/Exercise3/MobileDevice.h | 14 | ||||
| -rw-r--r-- | week12/Exercise3/Pager.cpp | 59 | ||||
| -rw-r--r-- | week12/Exercise3/Pager.h | 19 | ||||
| -rw-r--r-- | week12/Exercise3/TelecommunicationCompany.cpp | 47 | ||||
| -rw-r--r-- | week12/Exercise3/TelecommunicationCompany.h | 18 | ||||
| -rw-r--r-- | week12/Exercise3/Telephone.cpp | 6 | ||||
| -rw-r--r-- | week12/Exercise3/Telephone.h | 6 |
14 files changed, 290 insertions, 0 deletions
diff --git a/week12/Exercise1/Publication.cpp b/week12/Exercise1/Publication.cpp new file mode 100644 index 0000000..872c3ba --- /dev/null +++ b/week12/Exercise1/Publication.cpp @@ -0,0 +1,5 @@ +#include "Publication.h" + +std::ostream& operator<<(std::ostream& ostr, const Publication& other) { + return ostr << "From " << other.authors << " in " << other.journal; +} diff --git a/week12/Exercise1/Publication.h b/week12/Exercise1/Publication.h new file mode 100644 index 0000000..9c9ee73 --- /dev/null +++ b/week12/Exercise1/Publication.h @@ -0,0 +1,11 @@ +#pragma once +#include <iostream> + +class Publication { + char authors[1024]; + char journal[256]; + +public: + friend std::ostream& operator<<(std::ostream& ostr, const Publication& other); + virtual ~Publication() = default; +}; diff --git a/week12/Exercise1/ScientificPaper.cpp b/week12/Exercise1/ScientificPaper.cpp new file mode 100644 index 0000000..a530bfc --- /dev/null +++ b/week12/Exercise1/ScientificPaper.cpp @@ -0,0 +1,5 @@ +#include "ScientificPaper.h" + +std::ostream& operator<<(std::ostream& ostr, const ScientificPaper& other) { + return ostr << (Publication)other << std::endl << (TextDocument)other; +} diff --git a/week12/Exercise1/ScientificPaper.h b/week12/Exercise1/ScientificPaper.h new file mode 100644 index 0000000..ab1b800 --- /dev/null +++ b/week12/Exercise1/ScientificPaper.h @@ -0,0 +1,12 @@ +#pragma once +#include "Publication.h" +#include "TextDocument.h" +#include <iostream> + +class ScientificPaper : Publication, public TextDocument { + char discipline[512]; + +public: + friend std::ostream& operator<<(std::ostream& ostr, const ScientificPaper& other); + virtual ~ScientificPaper() = default; +}; diff --git a/week12/Exercise1/TextDocument.cpp b/week12/Exercise1/TextDocument.cpp new file mode 100644 index 0000000..f5abf2c --- /dev/null +++ b/week12/Exercise1/TextDocument.cpp @@ -0,0 +1,53 @@ +#include "TextDocument.h" +#include <cstring> + +void TextDocument::free() { + delete[] text; +} + +void TextDocument::copyFrom(const TextDocument& other) { + this->len = other.len; + this->text = new char[len + 1]; + strcpy(this->text, other.text); +} + +TextDocument::TextDocument() { + this->text = nullptr; +} + +TextDocument::~TextDocument() { + free(); +} + +TextDocument::TextDocument(const TextDocument& other) { + copyFrom(other); +} + +TextDocument& TextDocument::operator=(const TextDocument& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +TextDocument::TextDocument(TextDocument&& other) { + this->len = other.len; + this->text = other.text; + other.text = nullptr; +} + +TextDocument& TextDocument::operator=(TextDocument&& other) { + if (this != &other) { + free(); + + this->len = other.len; + this->text = other.text; + other.text = nullptr; + } + return *this; +} + +std::ostream& operator<<(std::ostream& ostr, const TextDocument& other) { + return ostr << other.text; +} diff --git a/week12/Exercise1/TextDocument.h b/week12/Exercise1/TextDocument.h new file mode 100644 index 0000000..cda1a32 --- /dev/null +++ b/week12/Exercise1/TextDocument.h @@ -0,0 +1,20 @@ +#pragma once +#include <iostream> + +class TextDocument { + char* text; + unsigned len; + + void free(); + void copyFrom(const TextDocument& other); + +public: + TextDocument(); + ~TextDocument(); + TextDocument(const TextDocument& other); + TextDocument& operator=(const TextDocument& other); + TextDocument(TextDocument&& other); + TextDocument& operator=(TextDocument&& other); + + friend std::ostream& operator<<(std::ostream& ostr, const TextDocument& other); +}; diff --git a/week12/Exercise3/MobileDevice.cpp b/week12/Exercise3/MobileDevice.cpp new file mode 100644 index 0000000..9820658 --- /dev/null +++ b/week12/Exercise3/MobileDevice.cpp @@ -0,0 +1,15 @@ +#include "MobileDevice.h" +#include <cstring> +#include <iostream> + +bool MobileDevice::CanAccept(const char* hyperMessage) { + unsigned msgNumber = atoi(hyperMessage); + return msgNumber == phoneNumber; +} + +void MobileDevice::Accept(const char* hyperMessage) { + if (!CanAccept(hyperMessage)) { + throw "Can't accept message!"; + } + strncpy(textMessage, strchr(hyperMessage, ' ') + 1, 512); +} diff --git a/week12/Exercise3/MobileDevice.h b/week12/Exercise3/MobileDevice.h new file mode 100644 index 0000000..b9de538 --- /dev/null +++ b/week12/Exercise3/MobileDevice.h @@ -0,0 +1,14 @@ +#pragma once + +class MobileDevice { +protected: + unsigned phoneNumber; + char textMessage[512]; + +public: + virtual ~MobileDevice() = default; + + virtual void Show() = 0; + bool CanAccept(const char* hyperMessage); + void Accept(const char* hyperMessage); +}; diff --git a/week12/Exercise3/Pager.cpp b/week12/Exercise3/Pager.cpp new file mode 100644 index 0000000..18beb4b --- /dev/null +++ b/week12/Exercise3/Pager.cpp @@ -0,0 +1,59 @@ +#include "Pager.h" +#include <iostream> +#include <cstring> +#include <fstream> + +void Pager::free() { + delete[] fileName; +} + +void Pager::copyFrom(const Pager& other) { + this->fileName = new char[strlen(other.fileName) + 1]; + strcpy(this->fileName, other.fileName); +} + +Pager::Pager() { + this->fileName = nullptr; +} + +Pager::~Pager() { + free(); +} + +Pager::Pager(const Pager& other) { + copyFrom(other); +} + +Pager& Pager::operator=(const Pager& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Pager::Pager(Pager&& other) { + this->fileName = other.fileName; + other.fileName = nullptr; +} + +Pager& Pager::operator=(Pager&& other) { + if (this != &other) { + free(); + + this->fileName = other.fileName; + other.fileName = nullptr; + } + return *this; +} + +void Pager::Show() { + std::ofstream outFile(fileName); + if (!outFile.is_open()) { + throw "Coudldn't open file!"; + } + + outFile << textMessage << std::endl; + + outFile.close(); +} diff --git a/week12/Exercise3/Pager.h b/week12/Exercise3/Pager.h new file mode 100644 index 0000000..451112e --- /dev/null +++ b/week12/Exercise3/Pager.h @@ -0,0 +1,19 @@ +#pragma once +#include "MobileDevice.h" + +class Pager : public MobileDevice { + char* fileName; + + void free(); + void copyFrom(const Pager& other); + +public: + Pager(); + ~Pager(); + Pager(const Pager& other); + Pager& operator=(const Pager& other); + Pager(Pager&& other); + Pager& operator=(Pager&& other); + + virtual void Show() override; +}; diff --git a/week12/Exercise3/TelecommunicationCompany.cpp b/week12/Exercise3/TelecommunicationCompany.cpp new file mode 100644 index 0000000..b059edb --- /dev/null +++ b/week12/Exercise3/TelecommunicationCompany.cpp @@ -0,0 +1,47 @@ +#include "TelecommunicationCompany.h" +#include "MobileDevice.h" + +void TelecommunicationCompany::free() { + for (int i = 0; i < size; i++) { + delete devices[i]; + } + delete[] devices; +} + +void TelecommunicationCompany::resize() { + allocated *= 2; + MobileDevice** moreDevices = new MobileDevice*[allocated]; + for (int i = 0; i < size; i++) { + moreDevices[i] = devices[i]; + } + delete[] devices; + devices = moreDevices; +} + +TelecommunicationCompany::TelecommunicationCompany() { + devices = nullptr; + allocated = size = 0; +} + +TelecommunicationCompany::~TelecommunicationCompany() { + free(); +} + +TelecommunicationCompany::TelecommunicationCompany(TelecommunicationCompany&& other) { + this->size = other.size; + this->allocated = other.allocated; + this->devices = other.devices; + other.devices = nullptr; +} + +TelecommunicationCompany& TelecommunicationCompany::operator=(TelecommunicationCompany&& other) { + if (this != &other) { + free(); + + this->size = other.size; + this->allocated = other.allocated; + this->devices = other.devices; + other.devices = nullptr; + } + return *this; +} diff --git a/week12/Exercise3/TelecommunicationCompany.h b/week12/Exercise3/TelecommunicationCompany.h new file mode 100644 index 0000000..22ce731 --- /dev/null +++ b/week12/Exercise3/TelecommunicationCompany.h @@ -0,0 +1,18 @@ +#pragma once +#include "MobileDevice.h" + +class TelecommunicationCompany { + MobileDevice** devices; + unsigned size; + unsigned allocated; + + void resize(); + + void free(); + +public: + TelecommunicationCompany(); + ~TelecommunicationCompany(); + TelecommunicationCompany(TelecommunicationCompany&& other); + TelecommunicationCompany& operator=(TelecommunicationCompany&& other); +}; diff --git a/week12/Exercise3/Telephone.cpp b/week12/Exercise3/Telephone.cpp new file mode 100644 index 0000000..1250616 --- /dev/null +++ b/week12/Exercise3/Telephone.cpp @@ -0,0 +1,6 @@ +#include "Telephone.h" +#include <iostream> + +void Telephone::Show() { + std::cout << textMessage << std::endl; +} diff --git a/week12/Exercise3/Telephone.h b/week12/Exercise3/Telephone.h new file mode 100644 index 0000000..aac94e6 --- /dev/null +++ b/week12/Exercise3/Telephone.h @@ -0,0 +1,6 @@ +#pragma once +#include "MobileDevice.h" + +class Telephone : public MobileDevice { + virtual void Show() override; +}; |
