diff options
| author | Syndamia <kamen@syndamia.com> | 2024-05-08 10:07:57 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-05-08 10:07:57 +0300 |
| commit | 75cff007b4896eed49b758be0959668673ae1bf3 (patch) | |
| tree | 6af017d6047c78bce05af166fdbe85cae40692b5 | |
| parent | 6dd52a7c9d8db3e8b14494ae99e8b9f3b76d1262 (diff) | |
| download | oop-2023-solutions-75cff007b4896eed49b758be0959668673ae1bf3.tar oop-2023-solutions-75cff007b4896eed49b758be0959668673ae1bf3.tar.gz oop-2023-solutions-75cff007b4896eed49b758be0959668673ae1bf3.zip | |
[w10] Fixed mistakes in certain exercises
| -rw-r--r-- | week10/Exercise02/Array.hpp | 10 | ||||
| -rw-r--r-- | week10/Exercise06/GradeWithName.cpp | 2 | ||||
| -rw-r--r-- | week10/Exercise08/Administrator.cpp | 33 | ||||
| -rw-r--r-- | week10/Exercise08/Moderator.cpp | 27 | ||||
| -rw-r--r-- | week10/Exercise08/Moderator.h | 6 | ||||
| -rw-r--r-- | week10/Exercise08/User.cpp | 12 | ||||
| -rw-r--r-- | week10/Exercise08/User.h | 8 | ||||
| -rw-r--r-- | week10/Exercise10/ShowableString.cpp | 10 | ||||
| -rw-r--r-- | week10/Exercise10/ShowableString.h | 2 |
9 files changed, 57 insertions, 53 deletions
diff --git a/week10/Exercise02/Array.hpp b/week10/Exercise02/Array.hpp index 114a1bc..6c2d95b 100644 --- a/week10/Exercise02/Array.hpp +++ b/week10/Exercise02/Array.hpp @@ -22,9 +22,13 @@ public: T& operator[](unsigned index); T& operator[](unsigned index) const; Array<T>& operator+=(const Array<T>& right); - friend bool operator==(const Array<T>& left, const Array<T>& right); - friend bool operator!=(const Array<T>& left, const Array<T>& right); - friend Array<T> operator+(const Array<T>& left, const Array<T>& right); + + template <class U> + friend bool operator==(const Array<U>& left, const Array<U>& right); + template <class U> + friend bool operator!=(const Array<U>& left, const Array<U>& right); + template <class U> + friend Array<U> operator+(const Array<U>& left, const Array<U>& right); }; template <class T> diff --git a/week10/Exercise06/GradeWithName.cpp b/week10/Exercise06/GradeWithName.cpp index 4a1b0b2..fb31a5f 100644 --- a/week10/Exercise06/GradeWithName.cpp +++ b/week10/Exercise06/GradeWithName.cpp @@ -21,7 +21,7 @@ GradeWithName::~GradeWithName() { free(); } -GradeWithName::GradeWithName(const GradeWithName& other) { +GradeWithName::GradeWithName(const GradeWithName& other) : Grade(other) { copyFrom(other); } diff --git a/week10/Exercise08/Administrator.cpp b/week10/Exercise08/Administrator.cpp index aaeecf8..23d9659 100644 --- a/week10/Exercise08/Administrator.cpp +++ b/week10/Exercise08/Administrator.cpp @@ -1,4 +1,5 @@ #include "Administrator.h" +#include <iostream> void Administrator::resize() { allocated *= 2; @@ -11,16 +12,14 @@ void Administrator::resize() { } void Administrator::free() { - Moderator::free(); delete[] arr; } void Administrator::copyFrom(const Administrator& other) { - Moderator::copyFrom(other); this->arr = new int[other.allocated]; for (int i = 0; i < other.size; i++) { this->arr[i] = other.arr[i]; -} + } } Administrator::Administrator() : Moderator() { @@ -29,28 +28,23 @@ Administrator::Administrator() : Moderator() { } Administrator::~Administrator() { - Administrator::free(); + free(); } -Administrator::Administrator(const Administrator& other) { - Administrator::copyFrom(other); +Administrator::Administrator(const Administrator& other) : Moderator(other) { + copyFrom(other); } Administrator& Administrator::operator=(const Administrator& other) { if (this != &other) { - Administrator::free(); - Administrator::copyFrom(other); + Moderator::operator=(other); + free(); + copyFrom(other); } return *this; } -Administrator::Administrator(Administrator&& other) { - this->name = other.name; - other.name = nullptr; - this->password = other.password; - other.password = nullptr; - this->signature = other.signature; - other.signature = nullptr; +Administrator::Administrator(Administrator&& other) : Moderator(other) { this->arr = other.arr; other.arr = nullptr; this->size = other.size; @@ -59,14 +53,9 @@ Administrator::Administrator(Administrator&& other) { Administrator& Administrator::operator=(Administrator&& other) { if (this != &other) { - Administrator::free(); + free(); + Moderator::operator=(std::move(other)); - this->name = other.name; - other.name = nullptr; - this->password = other.password; - other.password = nullptr; - this->signature = other.signature; - other.signature = nullptr; this->arr = other.arr; other.arr = nullptr; this->size = other.size; diff --git a/week10/Exercise08/Moderator.cpp b/week10/Exercise08/Moderator.cpp index d5d53b8..33ed638 100644 --- a/week10/Exercise08/Moderator.cpp +++ b/week10/Exercise08/Moderator.cpp @@ -1,13 +1,12 @@ #include "Moderator.h" #include <cstring> +#include <iostream> void Moderator::free() { - User::free(); delete[] signature; } void Moderator::copyFrom(const Moderator& other) { - User::copyFrom(other); this->signature = new char[strlen(other.signature) + 1]; strcpy(this->signature, other.signature); } @@ -17,38 +16,32 @@ Moderator::Moderator() : User() { } Moderator::~Moderator() { - Moderator::free(); + free(); } -Moderator::Moderator(const Moderator& other) { - Moderator::copyFrom(other); +Moderator::Moderator(const Moderator& other) : User(other) { + copyFrom(other); } Moderator& Moderator::operator=(const Moderator& other) { if (this != &other) { - Moderator::free(); - Moderator::copyFrom(other); + User::operator=(other); + free(); + copyFrom(other); } return *this; } -Moderator::Moderator(Moderator&& other) { - this->name = other.name; - other.name = nullptr; - this->password = other.password; - other.password = nullptr; +Moderator::Moderator(Moderator&& other) : User(other) { this->signature = other.signature; other.signature = nullptr; } Moderator& Moderator::operator=(Moderator&& other) { if (this != &other) { - Moderator::free(); + free(); + User::operator=(std::move(other)); - this->name = other.name; - other.name = nullptr; - this->password = other.password; - other.password = nullptr; this->signature = other.signature; other.signature = nullptr; } diff --git a/week10/Exercise08/Moderator.h b/week10/Exercise08/Moderator.h index 41bb5c6..70296a4 100644 --- a/week10/Exercise08/Moderator.h +++ b/week10/Exercise08/Moderator.h @@ -2,12 +2,12 @@ #include "User.h" class Moderator : public User { -protected: - char* signature; - void free(); void copyFrom(const Moderator& other); +protected: + char* signature; + public: Moderator(); ~Moderator(); diff --git a/week10/Exercise08/User.cpp b/week10/Exercise08/User.cpp index 663bfd3..8755e2c 100644 --- a/week10/Exercise08/User.cpp +++ b/week10/Exercise08/User.cpp @@ -52,6 +52,18 @@ const char* User::GetName() { return this->name; } +void User::SetName(const char* newName) { + delete[] name; + name = new char[strlen(newName) + 1]; + strcpy(name, newName); +} + const char* User::GetPassword() { return this->password; } + +void User::SetPassword(const char* newPassword) { + delete[] password; + password = new char[strlen(newPassword) + 1]; + strcpy(password, newPassword); +} diff --git a/week10/Exercise08/User.h b/week10/Exercise08/User.h index dd86c76..81a5fed 100644 --- a/week10/Exercise08/User.h +++ b/week10/Exercise08/User.h @@ -1,13 +1,13 @@ #pragma once class User { + void free(); + void copyFrom(const User& other); + protected: char* name; char* password; - void free(); - void copyFrom(const User& other); - public: User(); ~User(); @@ -17,5 +17,7 @@ public: User& operator=(User&& other); const char* GetName(); + void SetName(const char* newName); const char* GetPassword(); + void SetPassword(const char* newPassword); }; diff --git a/week10/Exercise10/ShowableString.cpp b/week10/Exercise10/ShowableString.cpp index 1231a9c..09dbd51 100644 --- a/week10/Exercise10/ShowableString.cpp +++ b/week10/Exercise10/ShowableString.cpp @@ -1,9 +1,13 @@ #include "ShowableString.h" std::ostream& operator<<(std::ostream& ostr, const ShowableString& str) { - return ostr << str; + return ostr << str.str; } -std::istream& operator>>(std::istream& istr, const ShowableString& str) { - return istr >> str; // Лошо! +std::istream& operator>>(std::istream& istr, ShowableString& str) { + unsigned size; + istr >> size; + str.free(); + str.str = new char[size]; + return istr >> str.str; } diff --git a/week10/Exercise10/ShowableString.h b/week10/Exercise10/ShowableString.h index ff46f6c..6bf02e6 100644 --- a/week10/Exercise10/ShowableString.h +++ b/week10/Exercise10/ShowableString.h @@ -5,5 +5,5 @@ class ShowableString : ModifiableString { public: friend std::ostream& operator<<(std::ostream& ostr, const ShowableString& str); - friend std::istream& operator>>(std::istream& istr, const ShowableString& str); + friend std::istream& operator>>(std::istream& istr, ShowableString& str); }; |
