aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise08
diff options
context:
space:
mode:
Diffstat (limited to 'week10/Exercise08')
-rw-r--r--week10/Exercise08/Administrator.cpp33
-rw-r--r--week10/Exercise08/Moderator.cpp27
-rw-r--r--week10/Exercise08/Moderator.h6
-rw-r--r--week10/Exercise08/User.cpp12
-rw-r--r--week10/Exercise08/User.h8
5 files changed, 41 insertions, 45 deletions
diff --git a/week10/Exercise08/Administrator.cpp b/week10/Exercise08/Administrator.cpp
index aaeecf8..23d9659 100644
--- a/week10/Exercise08/Administrator.cpp
+++ b/week10/Exercise08/Administrator.cpp
@@ -1,4 +1,5 @@
#include "Administrator.h"
+#include <iostream>
void Administrator::resize() {
allocated *= 2;
@@ -11,16 +12,14 @@ void Administrator::resize() {
}
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() {
@@ -29,28 +28,23 @@ Administrator::Administrator() : Moderator() {
}
Administrator::~Administrator() {
- Administrator::free();
+ free();
}
-Administrator::Administrator(const Administrator& other) {
- Administrator::copyFrom(other);
+Administrator::Administrator(const Administrator& other) : Moderator(other) {
+ copyFrom(other);
}
Administrator& Administrator::operator=(const Administrator& other) {
if (this != &other) {
- Administrator::free();
- Administrator::copyFrom(other);
+ Moderator::operator=(other);
+ free();
+ 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;
+Administrator::Administrator(Administrator&& other) : Moderator(other) {
this->arr = other.arr;
other.arr = nullptr;
this->size = other.size;
@@ -59,14 +53,9 @@ Administrator::Administrator(Administrator&& other) {
Administrator& Administrator::operator=(Administrator&& other) {
if (this != &other) {
- Administrator::free();
+ free();
+ Moderator::operator=(std::move(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;
diff --git a/week10/Exercise08/Moderator.cpp b/week10/Exercise08/Moderator.cpp
index d5d53b8..33ed638 100644
--- a/week10/Exercise08/Moderator.cpp
+++ b/week10/Exercise08/Moderator.cpp
@@ -1,13 +1,12 @@
#include "Moderator.h"
#include <cstring>
+#include <iostream>
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);
}
@@ -17,38 +16,32 @@ Moderator::Moderator() : User() {
}
Moderator::~Moderator() {
- Moderator::free();
+ free();
}
-Moderator::Moderator(const Moderator& other) {
- Moderator::copyFrom(other);
+Moderator::Moderator(const Moderator& other) : User(other) {
+ copyFrom(other);
}
Moderator& Moderator::operator=(const Moderator& other) {
if (this != &other) {
- Moderator::free();
- Moderator::copyFrom(other);
+ User::operator=(other);
+ free();
+ copyFrom(other);
}
return *this;
}
-Moderator::Moderator(Moderator&& other) {
- this->name = other.name;
- other.name = nullptr;
- this->password = other.password;
- other.password = nullptr;
+Moderator::Moderator(Moderator&& other) : User(other) {
this->signature = other.signature;
other.signature = nullptr;
}
Moderator& Moderator::operator=(Moderator&& other) {
if (this != &other) {
- Moderator::free();
+ free();
+ User::operator=(std::move(other));
- this->name = other.name;
- other.name = nullptr;
- this->password = other.password;
- other.password = nullptr;
this->signature = other.signature;
other.signature = nullptr;
}
diff --git a/week10/Exercise08/Moderator.h b/week10/Exercise08/Moderator.h
index 41bb5c6..70296a4 100644
--- a/week10/Exercise08/Moderator.h
+++ b/week10/Exercise08/Moderator.h
@@ -2,12 +2,12 @@
#include "User.h"
class Moderator : public User {
-protected:
- char* signature;
-
void free();
void copyFrom(const Moderator& other);
+protected:
+ char* signature;
+
public:
Moderator();
~Moderator();
diff --git a/week10/Exercise08/User.cpp b/week10/Exercise08/User.cpp
index 663bfd3..8755e2c 100644
--- a/week10/Exercise08/User.cpp
+++ b/week10/Exercise08/User.cpp
@@ -52,6 +52,18 @@ const char* User::GetName() {
return this->name;
}
+void User::SetName(const char* newName) {
+ delete[] name;
+ name = new char[strlen(newName) + 1];
+ strcpy(name, newName);
+}
+
const char* User::GetPassword() {
return this->password;
}
+
+void User::SetPassword(const char* newPassword) {
+ delete[] password;
+ password = new char[strlen(newPassword) + 1];
+ strcpy(password, newPassword);
+}
diff --git a/week10/Exercise08/User.h b/week10/Exercise08/User.h
index dd86c76..81a5fed 100644
--- a/week10/Exercise08/User.h
+++ b/week10/Exercise08/User.h
@@ -1,13 +1,13 @@
#pragma once
class User {
+ void free();
+ void copyFrom(const User& other);
+
protected:
char* name;
char* password;
- void free();
- void copyFrom(const User& other);
-
public:
User();
~User();
@@ -17,5 +17,7 @@ public:
User& operator=(User&& other);
const char* GetName();
+ void SetName(const char* newName);
const char* GetPassword();
+ void SetPassword(const char* newPassword);
};