Website Summary Files Commits Branches @commit @diff @stats
path: root / Utills / si_14.06 / task1
diff options
context:
space:
mode:
Commitb4e603d6018a0b43ad3ce9d3a1900ce784a2d07b (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2021-06-16 16:56:02 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2021-06-16 16:56:02 +0300
Files10cdb0ce4b62990ff27d418d29c659b7cf748fed /Utills/si_14.06/task1
Object-oriented_programming_FMI-b4e603d6018a0b43ad3ce9d3a1900ce784a2d07b.tar
Object-oriented_programming_FMI-b4e603d6018a0b43ad3ce9d3a1900ce784a2d07b.tar.gz
Object-oriented_programming_FMI-b4e603d6018a0b43ad3ce9d3a1900ce784a2d07b.zip
Parent9a86536fb579b244b29aa03fbefcce54746546dc (diff)
utills
2 files changed, 126 insertions, 0 deletions
-rw-r--r--Utills/si_14.06/task1/Optional/Optional.hpp109
-rw-r--r--Utills/si_14.06/task1/Source.cpp17
diff --git a/Utills/si_14.06/task1/Optional/Optional.hpp b/Utills/si_14.06/task1/Optional/Optional.hpp
new file mode 100644
index 0000000..cf1ec0d
--- /dev/null
+++ b/Utills/si_14.06/task1/Optional/Optional.hpp
@@ -0,0 +1,109 @@
+#include <iostream>
+
+template <typename T>
+class Optional
+{
+ T* objPtr;
+
+ void copyFrom(const Optional<T>& other);
+ void free();
+
+public:
+ Optional();
+ Optional(const T& obj);
+
+ Optional(const Optional& other);
+ Optional<T>& operator=(const Optional<T>& other);
+
+ ~Optional();
+
+ void setValue(const T& obj);
+ T getValue() const;
+
+ bool containsValue() const;
+
+ bool operator==(const Optional<T>& other) const;
+ bool operator!=(const Optional<T>& other) const;
+};
+
+template <typename T>
+Optional<T>::Optional() : objPtr(nullptr)
+{};
+
+template <typename T>
+Optional<T>::Optional(const T& obj)
+{
+ objPtr = new T(obj);
+}
+
+template <typename T>
+void Optional<T>::setValue(const T& obj)
+{
+ delete objPtr;
+ objPtr = new T(obj);
+}
+
+template <typename T>
+T Optional<T>::getValue() const
+{
+ if (!obj)
+ throw std::exception("No value assigned!");
+ return *objPtr;
+}
+
+template <typename T>
+bool Optional<T>::containsValue() const
+{
+ return objPtr != nullptr;
+}
+
+template <typename T>
+void Optional<T>::copyFrom(const Optional<T>& other)
+{
+ if (!other.obj)
+ objPtr = nullptr;
+ else
+ objPtr = new T(*other.obj);
+}
+
+template <typename T>
+void Optional<T>::free()
+{
+ delete objPtr;
+}
+
+template <typename T>
+Optional<T>::Optional(const Optional<T>& other)
+{
+ copyFrom(other);
+}
+
+template <typename T>
+Optional<T>& Optional<T>::operator=(const Optional<T>& other)
+{
+ if (this != &other)
+ {
+ free();
+ copyFrom(other);
+ }
+
+ return *this;
+}
+
+template <typename T>
+Optional<T>::~Optional()
+{
+ free();
+}
+
+template <typename T>
+bool Optional<T>::operator==(const Optional<T>& other) const
+{
+ return (!objPtr && !other.objPtr) || (objPtr && other.objPtr && *objPtr == *other.objPtr);
+}
+
+template <typename T>
+bool Optional<T>::operator!=(const Optional<T>& other) const
+{
+ return !operator==(other);
+}
diff --git a/Utills/si_14.06/task1/Source.cpp b/Utills/si_14.06/task1/Source.cpp
new file mode 100644
index 0000000..d8397a0
--- /dev/null
+++ b/Utills/si_14.06/task1/Source.cpp
@@ -0,0 +1,17 @@
+#include <iostream>
+#include "Optional/Optional.hpp"
+
+
+
+int main()
+{
+ Optional<int> o;
+
+ std::cout << o.containsValue() << std::endl;
+
+ o.setValue(12);
+
+ Optional<int> o2(12);
+
+ std::cout << (o == o2) << std::endl;
+} \ No newline at end of file