diff options
Diffstat (limited to 'week11/Exercise08')
| -rw-r--r-- | week11/Exercise08/Link.cpp | 54 | ||||
| -rw-r--r-- | week11/Exercise08/Link.h | 19 | ||||
| -rw-r--r-- | week11/Exercise08/Location.cpp | 5 | ||||
| -rw-r--r-- | week11/Exercise08/Location.h | 10 | ||||
| -rw-r--r-- | week11/Exercise08/Message.cpp | 55 | ||||
| -rw-r--r-- | week11/Exercise08/Message.h | 25 |
6 files changed, 168 insertions, 0 deletions
diff --git a/week11/Exercise08/Link.cpp b/week11/Exercise08/Link.cpp new file mode 100644 index 0000000..d9941d5 --- /dev/null +++ b/week11/Exercise08/Link.cpp @@ -0,0 +1,54 @@ +#include "Link.h" +#include <cstring> + +void Link::free() { + delete[] textMessage; + delete[] address; +} + +void Link::copyFrom(const Link& other) { + this->address = new char[strlen(other.address) + 1]; + strcpy(this->address, other.address); +} + +Link::Link() : Message() { + this->address = nullptr; +} + +Link::~Link() { + free(); +} + +Link::Link(const Link& other) : Message(other) { + copyFrom(other); +} + +Link& Link::operator=(const Link& other) { + if (this != &other) { + Message::operator=(other); + free(); + copyFrom(other); + } + return *this; +} + +Link::Link(Link&& other) { + Message::operator=(std::move(other)); + this->address = other.address; + other.address = nullptr; +} + +Link& Link::operator=(Link&& other) { + if (this != &other) { + free(); + + Message::operator=(std::move(other)); + this->address = other.address; + other.address = nullptr; + } + return *this; +} + +unsigned Link::size() { + return (Length() + 1) + (strlen(address) + 1); +} diff --git a/week11/Exercise08/Link.h b/week11/Exercise08/Link.h new file mode 100644 index 0000000..4acb312 --- /dev/null +++ b/week11/Exercise08/Link.h @@ -0,0 +1,19 @@ +#pragma once +#include "Message.h" + +class Link : public Message { + char* address; + + void free(); + void copyFrom(const Link& other); + +public: + Link(); + virtual ~Link() override; + Link(const Link& other); + Link& operator=(const Link& other); + Link(Link&& other); + Link& operator=(Link&& other); + + virtual unsigned size() override; +}; diff --git a/week11/Exercise08/Location.cpp b/week11/Exercise08/Location.cpp new file mode 100644 index 0000000..c63f470 --- /dev/null +++ b/week11/Exercise08/Location.cpp @@ -0,0 +1,5 @@ +#include "Location.h" + +unsigned Location::size() { + return (Length() + 1) + sizeof(lattitude) + sizeof(longitude); +} diff --git a/week11/Exercise08/Location.h b/week11/Exercise08/Location.h new file mode 100644 index 0000000..fbbe7a3 --- /dev/null +++ b/week11/Exercise08/Location.h @@ -0,0 +1,10 @@ +#pragma once +#include "Message.h" + +class Location : public Message { + float longitude; + float lattitude; + +public: + virtual unsigned size() override; +}; diff --git a/week11/Exercise08/Message.cpp b/week11/Exercise08/Message.cpp new file mode 100644 index 0000000..12c91e9 --- /dev/null +++ b/week11/Exercise08/Message.cpp @@ -0,0 +1,55 @@ +#include "Message.h" +#include <cstring> +#include <iostream> + +void Message::free() { + delete[] textMessage; +} + +void Message::copyFrom(const Message& other) { + this->textMessage = new char[strlen(other.textMessage) + 1]; + strcpy(this->textMessage, other.textMessage); +} + +Message::Message() { + this->textMessage = nullptr; +} + +Message::~Message() { + free(); +} + +Message::Message(const Message& other) { + copyFrom(other); +} + +Message& Message::operator=(const Message& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Message::Message(Message&& other) { + this->textMessage = other.textMessage; + other.textMessage = nullptr; +} + +Message& Message::operator=(Message&& other) { + if (this != &other) { + free(); + + this->textMessage = other.textMessage; + other.textMessage = nullptr; + } + return *this; +} + +unsigned Message::Length() { + return strlen(textMessage); +} + +std::ostream& operator<<(std::ostream& ostr, const Message& obj) { + return ostr << obj.textMessage; +} diff --git a/week11/Exercise08/Message.h b/week11/Exercise08/Message.h new file mode 100644 index 0000000..2048263 --- /dev/null +++ b/week11/Exercise08/Message.h @@ -0,0 +1,25 @@ +#pragma once +#include <iostream> + +class Message { + void copyFrom(const Message& other); + void free(); + +protected: + char* textMessage; + +public: + Message(); + virtual ~Message(); + Message(const Message& other); + Message& operator=(const Message& other); + Message(Message&& other); + Message& operator=(Message&& other); + + unsigned Length(); + // Отговорът е не, няма разлика между operator<< на трите класа + // В условието е казано, че текстовото съобщение се показва на екрана, но не е казано че другите данни се + friend std::ostream& operator<<(std::ostream& ostr, const Message& obj); + + virtual unsigned size() = 0; +}; |
