aboutsummaryrefslogtreecommitdiff
path: root/week12/Exercise4/Person.cpp
blob: 5d1ec09a6012506116613f1eb7028e5f10208f1d (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
#include "Person.h"
#include <cstring>

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);
}