aboutsummaryrefslogtreecommitdiff
path: root/week12/Exercise1
diff options
context:
space:
mode:
Diffstat (limited to 'week12/Exercise1')
-rw-r--r--week12/Exercise1/Publication.cpp5
-rw-r--r--week12/Exercise1/Publication.h11
-rw-r--r--week12/Exercise1/ScientificPaper.cpp5
-rw-r--r--week12/Exercise1/ScientificPaper.h12
-rw-r--r--week12/Exercise1/TextDocument.cpp53
-rw-r--r--week12/Exercise1/TextDocument.h20
6 files changed, 106 insertions, 0 deletions
diff --git a/week12/Exercise1/Publication.cpp b/week12/Exercise1/Publication.cpp
new file mode 100644
index 0000000..872c3ba
--- /dev/null
+++ b/week12/Exercise1/Publication.cpp
@@ -0,0 +1,5 @@
+#include "Publication.h"
+
+std::ostream& operator<<(std::ostream& ostr, const Publication& other) {
+ return ostr << "From " << other.authors << " in " << other.journal;
+}
diff --git a/week12/Exercise1/Publication.h b/week12/Exercise1/Publication.h
new file mode 100644
index 0000000..9c9ee73
--- /dev/null
+++ b/week12/Exercise1/Publication.h
@@ -0,0 +1,11 @@
+#pragma once
+#include <iostream>
+
+class Publication {
+ char authors[1024];
+ char journal[256];
+
+public:
+ friend std::ostream& operator<<(std::ostream& ostr, const Publication& other);
+ virtual ~Publication() = default;
+};
diff --git a/week12/Exercise1/ScientificPaper.cpp b/week12/Exercise1/ScientificPaper.cpp
new file mode 100644
index 0000000..a530bfc
--- /dev/null
+++ b/week12/Exercise1/ScientificPaper.cpp
@@ -0,0 +1,5 @@
+#include "ScientificPaper.h"
+
+std::ostream& operator<<(std::ostream& ostr, const ScientificPaper& other) {
+ return ostr << (Publication)other << std::endl << (TextDocument)other;
+}
diff --git a/week12/Exercise1/ScientificPaper.h b/week12/Exercise1/ScientificPaper.h
new file mode 100644
index 0000000..ab1b800
--- /dev/null
+++ b/week12/Exercise1/ScientificPaper.h
@@ -0,0 +1,12 @@
+#pragma once
+#include "Publication.h"
+#include "TextDocument.h"
+#include <iostream>
+
+class ScientificPaper : Publication, public TextDocument {
+ char discipline[512];
+
+public:
+ friend std::ostream& operator<<(std::ostream& ostr, const ScientificPaper& other);
+ virtual ~ScientificPaper() = default;
+};
diff --git a/week12/Exercise1/TextDocument.cpp b/week12/Exercise1/TextDocument.cpp
new file mode 100644
index 0000000..f5abf2c
--- /dev/null
+++ b/week12/Exercise1/TextDocument.cpp
@@ -0,0 +1,53 @@
+#include "TextDocument.h"
+#include <cstring>
+
+void TextDocument::free() {
+ delete[] text;
+}
+
+void TextDocument::copyFrom(const TextDocument& other) {
+ this->len = other.len;
+ this->text = new char[len + 1];
+ strcpy(this->text, other.text);
+}
+
+TextDocument::TextDocument() {
+ this->text = nullptr;
+}
+
+TextDocument::~TextDocument() {
+ free();
+}
+
+TextDocument::TextDocument(const TextDocument& other) {
+ copyFrom(other);
+}
+
+TextDocument& TextDocument::operator=(const TextDocument& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+TextDocument::TextDocument(TextDocument&& other) {
+ this->len = other.len;
+ this->text = other.text;
+ other.text = nullptr;
+}
+
+TextDocument& TextDocument::operator=(TextDocument&& other) {
+ if (this != &other) {
+ free();
+
+ this->len = other.len;
+ this->text = other.text;
+ other.text = nullptr;
+ }
+ return *this;
+}
+
+std::ostream& operator<<(std::ostream& ostr, const TextDocument& other) {
+ return ostr << other.text;
+}
diff --git a/week12/Exercise1/TextDocument.h b/week12/Exercise1/TextDocument.h
new file mode 100644
index 0000000..cda1a32
--- /dev/null
+++ b/week12/Exercise1/TextDocument.h
@@ -0,0 +1,20 @@
+#pragma once
+#include <iostream>
+
+class TextDocument {
+ char* text;
+ unsigned len;
+
+ void free();
+ void copyFrom(const TextDocument& other);
+
+public:
+ TextDocument();
+ ~TextDocument();
+ TextDocument(const TextDocument& other);
+ TextDocument& operator=(const TextDocument& other);
+ TextDocument(TextDocument&& other);
+ TextDocument& operator=(TextDocument&& other);
+
+ friend std::ostream& operator<<(std::ostream& ostr, const TextDocument& other);
+};