-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.h
92 lines (70 loc) · 1.92 KB
/
Student.h
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
#pragma once
class Student
{
char* name = nullptr;
char* surname = nullptr;
Date birthdate;
public:
Student()
{
this->name = new char[200];
strcpy_s(this->name, 200, "IVAN");
this->surname = new char[200];
strcpy_s(this->surname, 200, "IVANOV");
this->birthdate.SetDate(1, 1, 1970);
}
Student(const char* name, const char* surname, Date birthdate)
{
this->name = new char[200];
strcpy_s(this->name, 200, name);
this->surname = new char[200];
strcpy_s(this->surname, 200, surname);
this->birthdate.SetDate(birthdate.GetDay(), birthdate.GetMonth(), birthdate.GetYear());
}
void Print() const
{
cout << "Name: " << name << " " << surname << "\nBirthdate: ";
birthdate.Print();
}
Student* CreateRandomStudent()
{
string names[] = {"Vasya", "Petya", "Masha"};
Date d;
d.SetDate(rand() % 28 + 1, rand() % 12 + 1, rand() % 100 + 1935);
Student* temp = new Student();
// temp->name = new char[200]; // ýòà ñòðîêà óæå åñòü â êîíñòðóêòîðå
strcpy_s(this->name, 200, names[rand() % 3].c_str());
// this->surname = new char[200]; // çäåñü ìîæíî áûëî áû íàïèñàòü êîä ãåíåðàöèè ñëó÷àéíîé ôàìèëèè, íî ìíå ëåíü
// strcpy_s(this->surname, 200, surname);
temp->birthdate = d;
return temp;
}
~Student()
{
delete[] name;
delete[] surname;
}
// êîíñòðóêòîð êîïèðîâàíèÿ
Student(const Student& original) : Student(original.name, original.surname, original.birthdate)
{
}
Student& operator= (const Student& original)
{
// ñèòóàöèÿ ñ ñàìîïðèñâàèâàíèåì (s = s)
if (this == &original) return *this;
if (name != nullptr)
{
delete[] name;
}
name = new char[strlen(original.name) + 1];
strcpy_s(name, strlen(original.name) + 1, original.name);
if (surname != nullptr)
{
delete[] surname;
}
surname = new char[strlen(original.surname) + 1];
strcpy_s(surname, strlen(original.surname) + 1, original.surname);
this->birthdate = original.birthdate;
return *this;
}
};