Website Summary Files Commits Branches @commit @diff @stats
path: root / Big4 / InformaticsStudent.cpp
blob: 907335557db29f3559e9604be1f107bbe152d8f8 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "InformaticsStudent.h"
#include <cstring>


bool hasDigits(const char* str,int len)
{
	for (int i = 0; i < len;i++)
	{
		if (str[i] >= '0' && str[i] <= '9')
			return true;
	}
	return false;
}

void InformaticsStudent::CopyFrom(const InformaticsStudent& other)
{
	int len = strlen(other.name);
	name = new char[len + 1];
	strcpy(name, other.name); //copy name
	fn = other.fn; //copy fn
}
void InformaticsStudent::Free()
{
	delete[] name;
	fn = 0;
}

InformaticsStudent::InformaticsStudent()
{
	name = nullptr;
	fn = 0;
}

InformaticsStudent::InformaticsStudent(const char* name, int fn) //При конструктура винаги КОПИРАМЕ подадения стринг за нашния нов обект.
{								 // Това е важно, защото не искаме някой друг да има достъп до него.
	setName(name);
	setFn(fn);
}


InformaticsStudent::InformaticsStudent(const InformaticsStudent& other)
{
	//Използваме функцията, която копира всичко от other в нашия обект.
	CopyFrom(other); 
	
	//Понеже това е конструктор, няма нужда да трия нищо от обекта, понеже няма как да е имало данни тук,защото сега го създаваме.
}

InformaticsStudent& InformaticsStudent::operator=(const InformaticsStudent& other)
{
	//За разлика от при Копи-контструктора , тука е имало данни , преди копирането	
	// (обектът да е съществувал). За това преди да копираме трябва да изтрием това, което е седяло тук.
	// Проверката на 44 ред е с цел да не стане нещо от сорта T1=T1. Това би изтрило данните в обекта, защото първо трием и после 
	// присвояваме вече изтритите данни.
	
	if (this != &other)
	{
		Free(); //трия своята памет

		CopyFrom(other); // копирам от другия обект
	}
	return *this;  // Връщаме указател към обекта, в който сме копирали, за може да chain-ваме  T1=T2=T3.
}

InformaticsStudent::~InformaticsStudent()
{
	Free();
}


void InformaticsStudent::setName(const char* name)
{
	delete[] name; //трием данните, които са били тук.
	int len = strlen(name);
	if (hasDigits(name, len))
	{
		this->name[0] = '\0';
		return;
	}
	this->name = new char[len + 1];
	strcpy(this->name, name);


}
void InformaticsStudent::setFn(int fn)
{
	if (fn < 45000 || fn >45999)
		fn = 45000;
	this->fn = fn;
}
const char* InformaticsStudent::getName() const
{
	return name;
}
int InformaticsStudent::getFn() const
{
	return fn;
}

void InformaticsStudent::printStudent() const
{
	std::cout << name << " fn: " << fn << std::endl;
}