From a82a54f081ce561e6bc6e9f82869f8b40a47e660 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 25 May 2022 19:59:16 +0300 Subject: Added my work from today's exam --- .../Exam 2/ex1/String.cpp | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp (limited to 'Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp') diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp new file mode 100644 index 0000000..693e638 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp @@ -0,0 +1,40 @@ +#include "String.h" +#include + +void String::copyFrom(const String& other) { + length = other.length; + str = new char[length + 1]; + strncpy(str, other.str, length + 1); +} + +void String::free() { + delete[] str; +} + +String::String(const char* str) { + length = strlen(str); + this->str = new char[length + 1]; + strncpy(this->str, str, length + 1); +} + +String::String() : String("") {} + +String::String(const String& other) { + copyFrom(other); +} + +String& String::operator=(const String& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +String::~String() { + free(); +} + +std::ostream& operator<<(std::ostream& ostr, const String& str) { + return ostr << str.str; +} -- cgit v1.3.1-23-gd82d