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/Exercise08 | |
| 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/Exercise08')
| -rw-r--r-- | week10/Exercise08/Administrator.cpp | 97 | ||||
| -rw-r--r-- | week10/Exercise08/Administrator.h | 24 | ||||
| -rw-r--r-- | week10/Exercise08/Moderator.cpp | 60 | ||||
| -rw-r--r-- | week10/Exercise08/Moderator.h | 20 | ||||
| -rw-r--r-- | week10/Exercise08/User.cpp | 57 | ||||
| -rw-r--r-- | week10/Exercise08/User.h | 21 |
6 files changed, 279 insertions, 0 deletions
diff --git a/week10/Exercise08/Administrator.cpp b/week10/Exercise08/Administrator.cpp new file mode 100644 index 0000000..aaeecf8 --- /dev/null +++ b/week10/Exercise08/Administrator.cpp @@ -0,0 +1,97 @@ +#include "Administrator.h" + +void Administrator::resize() { + allocated *= 2; + int* bigger = new int[allocated]; + for (int i = 0; i < size; i++) { + bigger[i] = arr[i]; + } + delete[] arr; + arr = bigger; +} + +void Administrator::free() { + Moderator::free(); + delete[] arr; +} + +void Administrator::copyFrom(const Administrator& other) { + Moderator::copyFrom(other); + this->arr = new int[other.allocated]; + for (int i = 0; i < other.size; i++) { + this->arr[i] = other.arr[i]; +} +} + +Administrator::Administrator() : Moderator() { + arr = nullptr; + size = allocated = 0; +} + +Administrator::~Administrator() { + Administrator::free(); +} + +Administrator::Administrator(const Administrator& other) { + Administrator::copyFrom(other); +} + +Administrator& Administrator::operator=(const Administrator& other) { + if (this != &other) { + Administrator::free(); + Administrator::copyFrom(other); + } + return *this; +} + +Administrator::Administrator(Administrator&& other) { + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; + this->signature = other.signature; + other.signature = nullptr; + this->arr = other.arr; + other.arr = nullptr; + this->size = other.size; + this->allocated = other.allocated; +} + +Administrator& Administrator::operator=(Administrator&& other) { + if (this != &other) { + Administrator::free(); + + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; + this->signature = other.signature; + other.signature = nullptr; + this->arr = other.arr; + other.arr = nullptr; + this->size = other.size; + this->allocated = other.allocated; + } + return *this; +} + +void Administrator::AddID(int value) { + if (allocated <= size) { + resize(); + } + + arr[size++] = value; +} + +void Administrator::RemoveID(int value) { + int i; + for (i = 0; i < size; i++) { + if (arr[i] == value) { + break; + } + } + for (; i < size - 1; i++) { + arr[i] = arr[i+1]; + } + size--; +} diff --git a/week10/Exercise08/Administrator.h b/week10/Exercise08/Administrator.h new file mode 100644 index 0000000..c0f9975 --- /dev/null +++ b/week10/Exercise08/Administrator.h @@ -0,0 +1,24 @@ +#pragma once +#include "Moderator.h" + +class Administrator : public Moderator { + int* arr; + unsigned size; + unsigned allocated; + + void resize(); + + void free(); + void copyFrom(const Administrator& other); + +public: + Administrator(); + ~Administrator(); + Administrator(const Administrator& other); + Administrator& operator=(const Administrator& other); + Administrator(Administrator&& other); + Administrator& operator=(Administrator&& other); + + void AddID(int value); + void RemoveID(int value); +}; diff --git a/week10/Exercise08/Moderator.cpp b/week10/Exercise08/Moderator.cpp new file mode 100644 index 0000000..d5d53b8 --- /dev/null +++ b/week10/Exercise08/Moderator.cpp @@ -0,0 +1,60 @@ +#include "Moderator.h" +#include <cstring> + +void Moderator::free() { + User::free(); + delete[] signature; +} + +void Moderator::copyFrom(const Moderator& other) { + User::copyFrom(other); + this->signature = new char[strlen(other.signature) + 1]; + strcpy(this->signature, other.signature); +} + +Moderator::Moderator() : User() { + signature = nullptr; +} + +Moderator::~Moderator() { + Moderator::free(); +} + +Moderator::Moderator(const Moderator& other) { + Moderator::copyFrom(other); +} + +Moderator& Moderator::operator=(const Moderator& other) { + if (this != &other) { + Moderator::free(); + Moderator::copyFrom(other); + } + return *this; +} + +Moderator::Moderator(Moderator&& other) { + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; + this->signature = other.signature; + other.signature = nullptr; +} + +Moderator& Moderator::operator=(Moderator&& other) { + if (this != &other) { + Moderator::free(); + + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; + this->signature = other.signature; + other.signature = nullptr; + } + return *this; +} + +const char* Moderator::GetSignature() { + return this->signature; +} diff --git a/week10/Exercise08/Moderator.h b/week10/Exercise08/Moderator.h new file mode 100644 index 0000000..41bb5c6 --- /dev/null +++ b/week10/Exercise08/Moderator.h @@ -0,0 +1,20 @@ +#pragma once +#include "User.h" + +class Moderator : public User { +protected: + char* signature; + + void free(); + void copyFrom(const Moderator& other); + +public: + Moderator(); + ~Moderator(); + Moderator(const Moderator& other); + Moderator& operator=(const Moderator& other); + Moderator(Moderator&& other); + Moderator& operator=(Moderator&& other); + + const char* GetSignature(); +}; diff --git a/week10/Exercise08/User.cpp b/week10/Exercise08/User.cpp new file mode 100644 index 0000000..663bfd3 --- /dev/null +++ b/week10/Exercise08/User.cpp @@ -0,0 +1,57 @@ +#include "User.h" +#include <cstring> + +void User::free() { + delete[] name; + delete[] password; +} + +void User::copyFrom(const User& other) { + this->name = new char[strlen(other.name) + 1]; + strcpy(this->name, other.name); + this->password = new char[strlen(other.password) + 1]; + strcpy(this->password, other.password); +} + +User::User() { + name = password = nullptr; +} + +User::~User() { + free(); +} + +User::User(const User& other) { + copyFrom(other); +} + +User& User::operator=(const User& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +User::User(User&& other) { + this->name = other.name; + other.name = nullptr; + this->password = other.password; + other.password = nullptr; +} + +User& User::operator=(User&& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +const char* User::GetName() { + return this->name; +} + +const char* User::GetPassword() { + return this->password; +} diff --git a/week10/Exercise08/User.h b/week10/Exercise08/User.h new file mode 100644 index 0000000..dd86c76 --- /dev/null +++ b/week10/Exercise08/User.h @@ -0,0 +1,21 @@ +#pragma once + +class User { +protected: + char* name; + char* password; + + void free(); + void copyFrom(const User& other); + +public: + User(); + ~User(); + User(const User& other); + User& operator=(const User& other); + User(User&& other); + User& operator=(User&& other); + + const char* GetName(); + const char* GetPassword(); +}; |
