diff options
added new examples
6 files changed, 204 insertions, 0 deletions
| -rw-r--r-- | Big4_and_inheritance/Students/GradesBook.cpp | 91 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/GradesBook.h | 27 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/Student.cpp | 10 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/Student.h | 15 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/User.cpp | 43 | ||||
| -rw-r--r-- | Big4_and_inheritance/Students/User.h | 18 |
diff --git a/Big4_and_inheritance/Students/GradesBook.cpp b/Big4_and_inheritance/Students/GradesBook.cpp new file mode 100644 index 0000000..6d3de27 --- /dev/null +++ b/Big4_and_inheritance/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) +{ + std::cout << "Copy contr of GradesBook" << std::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]; +} + +std::ostream& operator<<(std::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/Big4_and_inheritance/Students/GradesBook.h b/Big4_and_inheritance/Students/GradesBook.h new file mode 100644 index 0000000..e63e92f --- /dev/null +++ b/Big4_and_inheritance/Students/GradesBook.h @@ -0,0 +1,27 @@ +#pragma once +#include <iostream> + +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 std::ostream& operator<<(std::ostream& stream, GradesBook& obj); + +}; diff --git a/Big4_and_inheritance/Students/Student.cpp b/Big4_and_inheritance/Students/Student.cpp new file mode 100644 index 0000000..4734529 --- /dev/null +++ b/Big4_and_inheritance/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) +{ + b.AddGrade(grade); +}
\ No newline at end of file diff --git a/Big4_and_inheritance/Students/Student.h b/Big4_and_inheritance/Students/Student.h new file mode 100644 index 0000000..aafaf58 --- /dev/null +++ b/Big4_and_inheritance/Students/Student.h @@ -0,0 +1,15 @@ +#pragma once +#include "User.h" +#include "GradesBook.h" + +class Student : public User +{ +public: + int fn; + GradesBook b; + + Student(const char* name, int age, int fn); + void ReceiveGrade(int grade); + + +}; diff --git a/Big4_and_inheritance/Students/User.cpp b/Big4_and_inheritance/Students/User.cpp new file mode 100644 index 0000000..93d34b6 --- /dev/null +++ b/Big4_and_inheritance/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/Big4_and_inheritance/Students/User.h b/Big4_and_inheritance/Students/User.h new file mode 100644 index 0000000..400057b --- /dev/null +++ b/Big4_and_inheritance/Students/User.h @@ -0,0 +1,18 @@ +#pragma once + +#include <iostream> +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(); +}; |
