aboutsummaryrefslogtreecommitdiff
path: root/week05/Exercise3.cpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-03-28 09:43:24 +0200
committerSyndamia <kamen@syndamia.com>2024-03-28 09:43:24 +0200
commit70c2c3eab85bee3100ce1c749af03937a6e11e17 (patch)
treede11302f6187f2234485381af99cf131aa9e210b /week05/Exercise3.cpp
parent96fc3d9205fb4fd8ecff960f44ae2e3def929882 (diff)
downloadoop-2023-solutions-70c2c3eab85bee3100ce1c749af03937a6e11e17.tar
oop-2023-solutions-70c2c3eab85bee3100ce1c749af03937a6e11e17.tar.gz
oop-2023-solutions-70c2c3eab85bee3100ce1c749af03937a6e11e17.zip
[w5] Added solutions to exercises 1-7
Diffstat (limited to 'week05/Exercise3.cpp')
-rw-r--r--week05/Exercise3.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/week05/Exercise3.cpp b/week05/Exercise3.cpp
new file mode 100644
index 0000000..5373540
--- /dev/null
+++ b/week05/Exercise3.cpp
@@ -0,0 +1,45 @@
+#include <cstring>
+
+struct Email {
+private:
+ char address[128];
+ char* contents;
+
+ void free() {
+ delete[] contents;
+ }
+ void copyFrom(const Email& other) {
+ strcpy(this->address, other.address);
+
+ int contSize = strlen(other.contents);
+ this->contents = new char[contSize+1];
+ strcpy(this->contents, other.contents);
+ }
+
+public:
+ Email(char address[128], const char* contents) {
+ strcpy(this->address, address);
+
+ int contSize = strlen(contents);
+ this->contents = new char[contSize + 1];
+ strcpy(this->contents, contents);
+ }
+
+ Email() {
+ address[0] = '\0';
+ contents = nullptr;
+ }
+ ~Email() {
+ free();
+ }
+ Email(const Email& other) {
+ copyFrom(other);
+ }
+ Email& operator=(const Email& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+};