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/Exercise3/Pager.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 week12/Exercise3/Pager.cpp (limited to 'week12/Exercise3/Pager.cpp') 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 +#include +#include + +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(); +} -- cgit v1.2.3