blob: d1b6bcad50d585e812c3fb8dfad712a53c05bd2e (
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
|
#include "Teacher.h"
Teacher::Teacher(const char* name, int age, const char* const* subjects, size_t subjectsCount) : Person(name, age)
{
this->subjects = new char* [subjectsCount];
for (size_t i = 0; i < subjectsCount; i++)
{
this->subjects[i] = new char[strlen(subjects[i]) + 1];
strcpy(this->subjects[i], subjects[i]);
}
this->subjectsCount = subjectsCount;
}
void Teacher::free()
{
for (size_t i = 0; i < subjectsCount; i++)
delete[] subjects[i];
delete[] subjects;
}
void Teacher::copyFrom(const Teacher& other)
{
subjects = new char* [other.subjectsCount];
for (size_t i = 0; i < other.subjectsCount; i++)
{
subjects[i] = new char[strlen(other.subjects[i]) + 1];
strcpy(subjects[i], other.subjects[i]);
}
subjectsCount = other.subjectsCount;
}
Teacher::Teacher(const Teacher& other) : Person(other)
{
copyFrom(other); //only the teacher stuff is copied
}
Teacher& Teacher::operator=(const Teacher& other)
{
if (this != &other)
{
Person::operator=(other); //delete data of person + copy the data from person
free(); //teacher
copyFrom(other); //teacher
}
return *this;
}
Teacher::~Teacher()
{
free();
} // destr person
|