diff options
| author | Syndamia <kamen@syndamia.com> | 2024-04-22 15:43:49 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-04-22 15:43:49 +0300 |
| commit | f3406754705e508f57768d3d0d8e457ff5b7620c (patch) | |
| tree | 9f8743208732c21d5dcb51302feb29058edd8e58 /week08/Exercise7.cpp | |
| parent | f7ed9a8a6c31b17d54d2f37cc0f12b64f8e4b6d2 (diff) | |
| download | oop-2023-solutions-f3406754705e508f57768d3d0d8e457ff5b7620c.tar oop-2023-solutions-f3406754705e508f57768d3d0d8e457ff5b7620c.tar.gz oop-2023-solutions-f3406754705e508f57768d3d0d8e457ff5b7620c.zip | |
[w8] Added exercise descriptions and solutions to ex 1-7
Diffstat (limited to 'week08/Exercise7.cpp')
| -rw-r--r-- | week08/Exercise7.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/week08/Exercise7.cpp b/week08/Exercise7.cpp new file mode 100644 index 0000000..60640b0 --- /dev/null +++ b/week08/Exercise7.cpp @@ -0,0 +1,97 @@ +#include "Exercise7.h" +#include <cstring> + +void UserError::free() { + delete[] className; + delete[] propertyName; +} + +void UserError::copyFrom(const UserError& other) { + this->className = new char[strlen(other.className) + 1]; + strcpy(this->className, other.className); + this->propertyName = new char[strlen(other.propertyName) + 1]; + strcpy(this->propertyName, other.propertyName); +} + +UserError::UserError() { + className = propertyName = nullptr; + createdCount++; +} + +UserError::~UserError() { + free(); +} + +UserError::UserError(const UserError& other) { + copyFrom(other); + createdCount++; +} + +UserError& UserError::operator=(const UserError& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +UserError::UserError(UserError&& other) { + this->className = other.className; + other.className = nullptr; + this->propertyName = other.propertyName; + other.propertyName = nullptr; +} + +UserError& UserError::operator=(UserError&& other) { + if (this != &other) { + free(); + + this->className = other.className; + other.className = nullptr; + this->propertyName = other.propertyName; + other.propertyName = nullptr; + } + return *this; +} + +UserError::UserError(const char* className, const char* propertyName) { + this->className = new char[strlen(className) + 1]; + strcpy(this->className, className); + this->propertyName = new char[strlen(propertyName) + 1]; + strcpy(this->propertyName, propertyName); + createdCount++; +} + +std::ostream& operator<<(std::ostream& ostr, const UserError& right) { + return ostr << "Invalid user info in class " << right.className << " at " << right.propertyName << "!" << std::endl; +} + +int UserError::GetCreatedCount() { + return createdCount; +} + +NumberInput::NumberInput(int min, int max) { + std::cout << "Enter number [" << min << ", " << max << "]: "; + std::cin >> value; + if (value < min || max < value) { + throw UserError("NumberInput", "constructor"); + } +} + +int NumberInput::GetValue() { + return value; +} + +int main() { + while(true) { + try { + NumberInput a(5, 10); + std::cout << a.GetValue() * 2 << std::endl; + break; + } + catch (const UserError& error) { + std::cout << error; + } + } + std::cout << UserError::GetCreatedCount() << std::endl; +} |
