From eb6305c8b0a71a613a34edbcabdaf1fde259451d Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 10 May 2024 12:27:14 +0300 Subject: [w12] Finished ex 4 --- week12/Exercise4/Person.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 week12/Exercise4/Person.cpp (limited to 'week12/Exercise4/Person.cpp') diff --git a/week12/Exercise4/Person.cpp b/week12/Exercise4/Person.cpp new file mode 100644 index 0000000..5d1ec09 --- /dev/null +++ b/week12/Exercise4/Person.cpp @@ -0,0 +1,58 @@ +#include "Person.h" +#include + +void Person::free() { + delete[] name; +} + +void Person::copyFrom(const Person& other) { + this->age = other.age; + this->name = new char[strlen(other.name) + 1]; + strcpy(this->name, other.name); +} + +Person::Person() { + this->name = nullptr; + age = 0; +} + +Person::~Person() { + free(); +} + +Person::Person(const Person& other) { + copyFrom(other); +} + +Person& Person::operator=(const Person& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Person::Person(Person&& other) { + this->age = other.age; + this->name = other.name; + other.name = nullptr; +} + +Person& Person::operator=(Person&& other) { + if (this != &other) { + free(); + + this->age = other.age; + this->name = other.name; + other.name = nullptr; + } + return *this; +} + +bool operator==(const Person& left, const Person& right) { + return strcmp(left.name, right.name) == 0; +} + +bool operator!=(const Person& left, const Person& right) { + return !(left == right); +} -- cgit v1.2.3