aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise2/Indexable.hpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-25 16:08:17 +0300
committerSyndamia <kamen@syndamia.com>2024-05-25 16:08:17 +0300
commit9c11fababb9b6b194e646fbeb62d141aacf74c43 (patch)
treef1a294fcd66b22463b9424c838ff171db8539a15 /week13/Exercise2/Indexable.hpp
parentc6fae5f1a0cc8394b1de3b671f07de7b3d677c59 (diff)
downloadoop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar
oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar.gz
oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.zip
[w13] Added solutions
Diffstat (limited to 'week13/Exercise2/Indexable.hpp')
-rw-r--r--week13/Exercise2/Indexable.hpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/week13/Exercise2/Indexable.hpp b/week13/Exercise2/Indexable.hpp
new file mode 100644
index 0000000..dd5fb6b
--- /dev/null
+++ b/week13/Exercise2/Indexable.hpp
@@ -0,0 +1,25 @@
+#pragma once
+#include "Container.hpp"
+
+template <class T>
+class Indexable : virtual public Container<T> {
+public:
+ T& operator[](unsigned index);
+ const T& operator[](unsigned index) const;
+};
+
+template <class T>
+T& Indexable<T>::operator[](unsigned index) {
+ if (index > this->size) {
+ throw "Index out of range!";
+ }
+ return this->arr[index];
+}
+
+template <class T>
+const T& Indexable<T>::operator[](unsigned index) const {
+ if (index > this->size) {
+ throw "Index out of range!";
+ }
+ return this->arr[index];
+}