From 9829b368ce529df277397e3b465fc536fa65ebfd Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 14 Apr 2022 23:53:14 +0300 Subject: Implemented (broken) overall functionality of ex3 of sem 08 --- .../Solutions Sem. 08/ex3/DynStr.cpp | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp (limited to 'Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp') diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp new file mode 100644 index 0000000..4f4d52e --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 08/ex3/DynStr.cpp @@ -0,0 +1,50 @@ +#include "DynStr.h" +#include + +/* Private */ + +void DynStr::free() { + if(str != nullptr) delete[] str; +} + +void DynStr::copyFrom(const DynStr& other) { + length = other.length; + + str = new char[length]; + for (int i = 0; i < length; i++) + str[i] = other.str[i]; +} + +/* Public */ + +DynStr::DynStr() { + str = nullptr; + length = 0; +} + +DynStr::DynStr(const char* str) { + int strLen = strlen(str); + this->str = new char[strLen]; + strcpy(this->str, str); + length = strLen; +} + +DynStr::DynStr(const DynStr& copy) { + copyFrom(copy); +} + +DynStr& DynStr::operator=(const DynStr& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +DynStr::~DynStr() { + free(); +} + +const char* DynStr::get_str() const { + return str; +} -- cgit v1.3.1-23-gd82d