blob: 0c04b83a0f816758104de66c5075882419903bbe (
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
|
#include "GradesBook.h"
GradesBook::GradesBook()
{
capacity = 4;
grades = new int[capacity];
size = 0;
}
GradesBook::GradesBook(const GradesBook& other)
{
cout << "Copy contr of GradesBook" << endl;
CopyFrom(other);
}
GradesBook& GradesBook::operator=(const GradesBook& other)
{
if (this != &other)
{
Free();
CopyFrom(other);
}
return *this;
}
GradesBook::~GradesBook()
{
Free();
}
void GradesBook::CopyFrom(const GradesBook& other)
{
grades = new int[other.capacity];
for (int i = 0; i < other.size; i++)
grades[i] = other.grades[i];
capacity = other.capacity;
size = other.size;
}
void GradesBook::Free()
{
delete[] grades;
}
bool GradesBook::isFull()
{
return size == capacity;
}
void GradesBook::AddGrade(int grade)
{
if (!isValidGrade(grade))
return;
if (isFull())
Resize();
grades[size++] = grade;
}
void GradesBook::Resize()
{
int* temp = new int[capacity * 2];
for (int i = 0; i < size; i++)
temp[i] = grades[i];
capacity *= 2;
delete[] grades;
grades = temp;
}
void GradesBook::RemoveLastGrade()
{
size--;
}
bool GradesBook::isValidGrade(int grade)
{
return (grade >= 2 && grade <= 6);
}
int GradesBook::operator[](int index) const
{
if (index < 0 || index >= size)
return -1;
return grades[index];
}
ostream& operator<<(ostream& stream, GradesBook& obj)
{
for (int i = 0; i < obj.capacity; i++)
{
if (i < obj.size)
stream << obj.grades[i] << ' ';
else
stream << '_' << ' ';
}
return stream;
}
|