From 8dd9b39fa54a91030cdd5fe71973f78f7e8089e0 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 10 May 2024 11:33:50 +0300 Subject: [w12] Solved exercises 1 and 3 --- week12/Exercise1/TextDocument.cpp | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 week12/Exercise1/TextDocument.cpp (limited to 'week12/Exercise1/TextDocument.cpp') 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 + +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; +} -- cgit v1.2.3