aboutsummaryrefslogtreecommitdiff
path: root/week12/Exercise3/TelecommunicationCompany.cpp
blob: 34987a405cda2d4299698ccc8825fb06e1d394d7 (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
#include "TelecommunicationCompany.h"
#include "MobileDevice.h"
#include "Telephone.h"

void TelecommunicationCompany::resize() {
	allocated *= 2;
	MobileDevice** moreDevices = new MobileDevice*[allocated];
	for (int i = 0; i < size; i++) {
		moreDevices[i] = devices[i];
	}
	delete[] devices;
	devices = moreDevices;
}

void TelecommunicationCompany::free() {
	for (int i = 0; i < size; i++) {
		delete devices[i];
	}
	delete[] devices;
}

void TelecommunicationCompany::copyFrom(const TelecommunicationCompany& other) {
	this->allocated = other.allocated;
	this->size = other.size;
	this->devices = new MobileDevice*[size];
	for (int i = 0; i < size; i++) {
		this->devices[i] = other.devices[i]->clone();
	}
}

TelecommunicationCompany::TelecommunicationCompany() {
	devices = nullptr;
	allocated = size = 0;
}

TelecommunicationCompany::~TelecommunicationCompany() {
	free();
}

TelecommunicationCompany::TelecommunicationCompany(const TelecommunicationCompany& other) {
	copyFrom(other);
}

TelecommunicationCompany& TelecommunicationCompany::operator=(const TelecommunicationCompany& other) {
	if (this != &other) {
		copyFrom(other);
		free();
	}
	return *this;
}

TelecommunicationCompany::TelecommunicationCompany(TelecommunicationCompany&& other) {
	this->size = other.size;
	this->allocated = other.allocated;
	this->devices = other.devices;
	other.devices = nullptr;
}

TelecommunicationCompany& TelecommunicationCompany::operator=(TelecommunicationCompany&& other) {
	if (this != &other) {
		free();

		this->size = other.size;
		this->allocated = other.allocated;
		this->devices = other.devices;
		other.devices = nullptr;
	}
	return *this;
}