From 71c57cde42e99301aeb079be4ef368ff155f99c5 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 25 May 2022 14:05:56 +0300 Subject: Added my prep work for exam2 --- .../Exam 2 Preparation/String.cpp | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp (limited to 'Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp') diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp new file mode 100644 index 0000000..84c0046 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/String.cpp @@ -0,0 +1,36 @@ +#include "String.h" +#include + +void String::copyFrom(const String& other) { + length = other.length; + str = new char[length]; + strncpy(str, other.str, length); +} + +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(); +} -- cgit v1.3.1-23-gd82d