aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise08/Administrator.cpp
blob: aaeecf8e25e755dd78a57747148d9e6acaea8671 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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--;
}