aboutsummaryrefslogtreecommitdiff
path: root/week11/Exercise04
diff options
context:
space:
mode:
Diffstat (limited to 'week11/Exercise04')
-rw-r--r--week11/Exercise04/Name.cpp5
-rw-r--r--week11/Exercise04/Name.h9
-rw-r--r--week11/Exercise04/Street.cpp17
-rw-r--r--week11/Exercise04/Street.h9
-rw-r--r--week11/Exercise04/String.cpp50
-rw-r--r--week11/Exercise04/String.h18
6 files changed, 108 insertions, 0 deletions
diff --git a/week11/Exercise04/Name.cpp b/week11/Exercise04/Name.cpp
new file mode 100644
index 0000000..c12dfe7
--- /dev/null
+++ b/week11/Exercise04/Name.cpp
@@ -0,0 +1,5 @@
+#include "Name.h"
+
+unsigned Name::Length() {
+ return String::Length() * nameNumber;
+}
diff --git a/week11/Exercise04/Name.h b/week11/Exercise04/Name.h
new file mode 100644
index 0000000..3120dd7
--- /dev/null
+++ b/week11/Exercise04/Name.h
@@ -0,0 +1,9 @@
+#pragma once
+#include "String.h"
+
+class Name : public String {
+ unsigned nameNumber;
+
+public:
+ virtual unsigned Length() override;
+};
diff --git a/week11/Exercise04/Street.cpp b/week11/Exercise04/Street.cpp
new file mode 100644
index 0000000..8cc6905
--- /dev/null
+++ b/week11/Exercise04/Street.cpp
@@ -0,0 +1,17 @@
+#include "Street.h"
+
+unsigned numberLength(unsigned num) {
+ if (num == 0) return 1;
+
+ unsigned len = 0;
+ while (num != 0) {
+ len++;
+ num /= 10;
+ }
+
+ return len;
+}
+
+unsigned Street::Length() {
+ return String::Length() + numberLength(streetNumber);
+}
diff --git a/week11/Exercise04/Street.h b/week11/Exercise04/Street.h
new file mode 100644
index 0000000..279198e
--- /dev/null
+++ b/week11/Exercise04/Street.h
@@ -0,0 +1,9 @@
+#pragma once
+#include "String.h"
+
+class Street : public String {
+ unsigned streetNumber;
+
+public:
+ virtual unsigned Length() override;
+};
diff --git a/week11/Exercise04/String.cpp b/week11/Exercise04/String.cpp
new file mode 100644
index 0000000..e9cdd94
--- /dev/null
+++ b/week11/Exercise04/String.cpp
@@ -0,0 +1,50 @@
+#include "String.h"
+#include <cstring>
+
+void String::free() {
+ delete[] str;
+}
+
+void String::copyFrom(const String& other) {
+ this->str = new char[strlen(other.str) + 1];
+ strcpy(this->str, other.str);
+}
+
+String::String() {
+ this->str = nullptr;
+}
+
+String::~String() {
+ free();
+}
+
+String::String(const String& other) {
+ copyFrom(other);
+}
+
+String& String::operator=(const String& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+String::String(String&& other) {
+ this->str = other.str;
+ other.str = nullptr;
+}
+
+String& String::operator=(String&& other) {
+ if (this != &other) {
+ free();
+
+ this->str = other.str;
+ other.str = nullptr;
+ }
+ return *this;
+}
+
+unsigned String::Length() {
+ return strlen(str);
+}
diff --git a/week11/Exercise04/String.h b/week11/Exercise04/String.h
new file mode 100644
index 0000000..13481a0
--- /dev/null
+++ b/week11/Exercise04/String.h
@@ -0,0 +1,18 @@
+#pragma once
+
+class String {
+ char *str;
+
+ void free();
+ void copyFrom(const String& other);
+
+public:
+ String();
+ ~String();
+ String(const String& other);
+ String& operator=(const String& other);
+ String(String&& other);
+ String& operator=(String&& other);
+
+ virtual unsigned Length();
+};