From 9c11fababb9b6b194e646fbeb62d141aacf74c43 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 25 May 2024 16:08:17 +0300 Subject: [w13] Added solutions --- week13/Exercise1/String.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 week13/Exercise1/String.cpp (limited to 'week13/Exercise1/String.cpp') diff --git a/week13/Exercise1/String.cpp b/week13/Exercise1/String.cpp new file mode 100644 index 0000000..2b41e96 --- /dev/null +++ b/week13/Exercise1/String.cpp @@ -0,0 +1,51 @@ +#include "String.h" +#include + +void String::free() { + delete[] str; +} + +void String::copyFrom(const String& other) { + this->str = new char[strlen(other.str) + 1]; + strcpy(this->str, other.str); +} + +String::String() { + this->str = nullptr; +} + +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) { + this->str = other.str; + other.str = nullptr; +} + +String& String::operator=(String&& other) { + if (this != &other) { + free(); + + this->str = other.str; + other.str = nullptr; + } + return *this; +} + +String::String(const char* str) { + this->str = new char[strlen(str) + 1]; + strcpy(this->str, str); +} -- cgit v1.2.3