aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise06/GradeWithName.cpp
blob: fb31a5f6cff02571c6a5d50feb3e176fa95e0ce9 (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
#include "GradeWithName.h"
#include "Grade.h"
#include <cstring>

void GradeWithName::free() {
	delete[] name;
}

void GradeWithName::copyFrom(const GradeWithName& other) {
	this->numericValue = other.numericValue;
	this->name = new char[strlen(other.name) + 1];
	strcpy(this->name, other.name);
}

GradeWithName::GradeWithName(unsigned numericValue, const char* name) : Grade(numericValue) {
	this->name = new char[strlen(name) + 1];
	strcpy(this->name, name);
}

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

GradeWithName::GradeWithName(const GradeWithName& other) : Grade(other) {
	copyFrom(other);
}

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

GradeWithName::GradeWithName(GradeWithName&& other) : Grade(other.numericValue) {
	this->name = other.name;
	other.name = nullptr;
}

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

		this->numericValue = other.numericValue;
		this->name = other.name;
		other.name = nullptr;
	}
	return *this;
}