From 97635dc430c989791c3e17fa8a633e87033d29b6 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Fri, 20 Sep 2019 15:00:26 +0300 Subject: Add files via upload --- Classes/GradesBook(DynamicArray)/GradesBook.cpp | 91 +++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Classes/GradesBook(DynamicArray)/GradesBook.cpp (limited to 'Classes/GradesBook(DynamicArray)/GradesBook.cpp') diff --git a/Classes/GradesBook(DynamicArray)/GradesBook.cpp b/Classes/GradesBook(DynamicArray)/GradesBook.cpp new file mode 100644 index 0000000..0c04b83 --- /dev/null +++ b/Classes/GradesBook(DynamicArray)/GradesBook.cpp @@ -0,0 +1,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; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d