diff options
| author | Syndamia <kamen@syndamia.com> | 2024-05-07 22:17:12 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-05-07 22:17:12 +0300 |
| commit | 88f15e35713c9632216931d26443dc588238732f (patch) | |
| tree | 147ff8c72009afc58d3eabb1c026a33b798f20fe /week10/Exercise07/String.cpp | |
| parent | f4642325f58172e85b9e41e35f69e8bea46f78c6 (diff) | |
| download | oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar.gz oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.zip | |
[w10] Added rough solutions to ex 1-10
Diffstat (limited to 'week10/Exercise07/String.cpp')
| -rw-r--r-- | week10/Exercise07/String.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/week10/Exercise07/String.cpp b/week10/Exercise07/String.cpp new file mode 100644 index 0000000..a9276c3 --- /dev/null +++ b/week10/Exercise07/String.cpp @@ -0,0 +1,67 @@ +#include "String.h" +#include <cstring> + +void String::free() { + delete[] str; +} + +void String::copyFrom(const String& other) { + this->length = other.length; + this->str = new char[strlen(other.str) + 1]; + strcpy(this->str, other.str); +} + +String::String(const char* str) { + this->length = strlen(str); + this->str = new char[this->length + 1]; + strcpy(this->str, str); +} + +String::String() { + this->str = nullptr; + this->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) { + this->length = other.length; + this->str = other.str; + other.str = nullptr; +} + +String& String::operator=(String&& other) { + if (this != &other) { + free(); + + this->length = other.length; + this->str = other.str; + other.str = nullptr; + } + return *this; +} + +char& String::At(unsigned index) { + if (index >= length) { + throw "Index too big!"; + } + return str[index]; +} + +const char* String::GetPtr() { + return str; +} |
