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/Exercise10/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/Exercise10/String.cpp')
| -rw-r--r-- | week10/Exercise10/String.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/week10/Exercise10/String.cpp b/week10/Exercise10/String.cpp new file mode 100644 index 0000000..7fd689f --- /dev/null +++ b/week10/Exercise10/String.cpp @@ -0,0 +1,71 @@ +#include "String.h" +#include <cstring> + +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() { + 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; +} + +String& String::operator=(String&& other) { + if (this != &other) { + free(); + + this->str = other.str; + } + return *this; +} + +bool operator==(const String& left, const String& right) { + int i; + for (i = 0; left.str[i] != '\0' && right.str[i] != '\0'; i++) { + if (left.str[i] != right.str[i]) + return false; + } + return left.str[i] == '\0' && right.str[i] == '\0'; +} + +bool operator!=(const String& left, const String& right) { + return !(left == right); +} + +String operator+(const String& left, const String& right) { + String concat; + concat.str = new char[strlen(left.str) + strlen(right.str) + 1]; + int i; + for (i = 0; left.str[i] != '\0'; i++) { + concat.str[i] = left.str[i]; + } + int j; + for (j = 0; right.str[j] != '\0'; j++) { + concat.str[i+j] = right.str[j]; + } + return concat; +} |
