From b38346eebca9a007971af02fb3bb9472c5e16d0c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 26 Apr 2026 21:43:31 +0300 Subject: feat(Week09): Add solutions --- Week09/Exercise05/String.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Week09/Exercise05/String.cpp (limited to 'Week09/Exercise05/String.cpp') diff --git a/Week09/Exercise05/String.cpp b/Week09/Exercise05/String.cpp new file mode 100644 index 0000000..903aa60 --- /dev/null +++ b/Week09/Exercise05/String.cpp @@ -0,0 +1,75 @@ +#include "String.h" +#include +#include + +void String::free() { + delete[] str; +} + +void String::copyFrom(const String& other) { + length = other.length; + // Не забравяме за терминиращата нула + str = new char[length + 1]; + strcpy(str, other.str); +} + +String::String() { + str = nullptr; + length = 0; +} + +String::~String() { + free(); +} + +String::String(const String& other) { + copyFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +String::String(String&& other) { + str = other.str; + length = other.length; + + other.str = nullptr; + other.length = 0; +} + +String& String::operator=(String&& other) { + if (this != &other) { + free(); + + str = other.str; + length = other.length; + + other.str = nullptr; + other.length = 0; + } + return *this; +} + +String::String(unsigned length) { + this->length = length; + this->str = new char[length + 1]; + this->str[length] = '\0'; +} + +void String::Copy(const char* str) { + if (this->str == nullptr) { + length = strlen(str); + this->str = new char[length + 1]; + } + strncpy(this->str, str, length); + this->str[length] = '\0'; +} + +const char* String::GetStr() const { + return str; +} -- cgit v1.3.1-23-gd82d