From 335c0fca5c74c263c8ff21b2db193b423e46c4fe Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Fri, 20 Sep 2019 14:56:56 +0300 Subject: Add files via upload --- Big4AndInheritance/Students/GradesBook.cpp | 91 ++++++++++++++++++++++++++++++ Big4AndInheritance/Students/GradesBook.h | 24 ++++++++ Big4AndInheritance/Students/Student.cpp | 10 ++++ Big4AndInheritance/Students/Student.h | 13 +++++ Big4AndInheritance/Students/User.cpp | 43 ++++++++++++++ Big4AndInheritance/Students/User.h | 17 +++++- 6 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 Big4AndInheritance/Students/GradesBook.cpp create mode 100644 Big4AndInheritance/Students/GradesBook.h create mode 100644 Big4AndInheritance/Students/Student.cpp create mode 100644 Big4AndInheritance/Students/Student.h create mode 100644 Big4AndInheritance/Students/User.cpp diff --git a/Big4AndInheritance/Students/GradesBook.cpp b/Big4AndInheritance/Students/GradesBook.cpp new file mode 100644 index 0000000..0c04b83 --- /dev/null +++ b/Big4AndInheritance/Students/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 diff --git a/Big4AndInheritance/Students/GradesBook.h b/Big4AndInheritance/Students/GradesBook.h new file mode 100644 index 0000000..c9ba8e9 --- /dev/null +++ b/Big4AndInheritance/Students/GradesBook.h @@ -0,0 +1,24 @@ +class GradesBook +{ + int* grades; + int size; + int capacity; + + bool isValidGrade(int grade); + void Resize(); + void CopyFrom(const GradesBook& other); + void Free(); +public: + GradesBook(); + GradesBook(const GradesBook& other); + GradesBook& operator=(const GradesBook& other); + ~GradesBook(); + + bool isFull(); + void AddGrade(int grade); + void RemoveLastGrade(); + + int operator[](int index) const; + friend ostream& operator<<(ostream& stream, GradesBook& obj); + +}; diff --git a/Big4AndInheritance/Students/Student.cpp b/Big4AndInheritance/Students/Student.cpp new file mode 100644 index 0000000..c7180c8 --- /dev/null +++ b/Big4AndInheritance/Students/Student.cpp @@ -0,0 +1,10 @@ +#include "Student.h" +Student::Student(const char* name, int age, int fn): User(name,age) +{ + this->fn = fn; +} + +void Student::ReceiveGrade(int grade) +{ + book.AddGrade(grade); +} \ No newline at end of file diff --git a/Big4AndInheritance/Students/Student.h b/Big4AndInheritance/Students/Student.h new file mode 100644 index 0000000..4921a5d --- /dev/null +++ b/Big4AndInheritance/Students/Student.h @@ -0,0 +1,13 @@ +#include "User.h" + +class Student : public User +{ +public: + int fn; + GradesBook b; + + Student(const char* name, int age, int fn); + void ReceiveGrade(int grade) + + +}; \ No newline at end of file diff --git a/Big4AndInheritance/Students/User.cpp b/Big4AndInheritance/Students/User.cpp new file mode 100644 index 0000000..27947b1 --- /dev/null +++ b/Big4AndInheritance/Students/User.cpp @@ -0,0 +1,43 @@ +#include "User.h" + +/* +User::User(): name(nullptr),age(10) +{ + +} +*/ + +User::User(const char* name, int age) +{ + this->name = new char[strlen(name) + 1]; + strcpy(this->name, name); + this->age = age; +} +User::User(const User& other) +{ + cout << "Copy contr of user" << endl; + CopyFrom(other); +} +User& User::operator=(const User& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +User::~User() +{ + Free(); +} +void User::CopyFrom(const User& other) +{ + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); + age = other.age; +} +void User::Free() +{ + delete[] name; +} \ No newline at end of file diff --git a/Big4AndInheritance/Students/User.h b/Big4AndInheritance/Students/User.h index 4bcfe98..c466ef4 100644 --- a/Big4AndInheritance/Students/User.h +++ b/Big4AndInheritance/Students/User.h @@ -1 +1,16 @@ -d +#include +using namespace std; +class User +{ +private: + char* name; + int age; + void CopyFrom(const User& other); + void Free(); +public: + //User(); + User(const char*name, int age); + User(const User& other); + User& operator=(const User& other); + ~User(); +}; -- cgit v1.3.1-23-gd82d