Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
diff options
context:
space:
mode:
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/CheckBox.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/CheckBox.h
new file mode 100644
index 0000000..b72089c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/CheckBox.h
@@ -0,0 +1,14 @@
+#include "Control.h"
+#include "String.h"
+#include <iostream>
+
+class CheckBox : public Control {
+ String str;
+ bool status;
+
+public:
+ void setDataDialog() override {
+ std::cout << "Should be turned on? [0/1] ";
+ std::cin >> status;
+ }
+};
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Control.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Control.h
new file mode 100644
index 0000000..ba56100
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Control.h
@@ -0,0 +1,17 @@
+#include "Location.h"
+#include "Size.h"
+
+class Control {
+ Location location;
+ Size size;
+
+public:
+ virtual void setDataDialog() = 0;
+ virtual void changeSize(Size newSize) {
+ size = newSize;
+ }
+
+ Location get_loc() const {
+ return location;
+ }
+};
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Form.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Form.h
new file mode 100644
index 0000000..7818d58
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Form.h
@@ -0,0 +1,29 @@
+#include "String.h"
+#include "List.hpp"
+#include "Control.h"
+#include "Size.h"
+
+class Form {
+ Size size;
+ String name;
+ List<Control*> controls;
+
+public:
+ void addControl(Control& newControl) {
+ controls.add(&newControl);
+ }
+ void changeSize(Size newSize);
+ Control& operator[](unsigned index) {
+ return *controls[index];
+ }
+ bool changeControlLocation(unsigned index, Location newLoc) {
+ if (newLoc.x >= 0 && newLoc.x <= size.width && newLoc.y >= 0 && newLoc.y <= size.height) {
+ controls[index]->get_loc() = newLoc;
+ return true;
+ }
+ return false;
+ }
+ void change(unsigned index) {
+ controls[index]->setDataDialog();
+ }
+};
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Label.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Label.h
new file mode 100644
index 0000000..093ddab
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Label.h
@@ -0,0 +1,15 @@
+#include "Control.h"
+#include "String.h"
+#include <iostream>
+
+class Label : public Control {
+ String str;
+
+public:
+ void setDataDialog() override {
+ std::cout << "Enter new label: ";
+ char buffer[1024];
+ std::cin.getline(buffer, 1024);
+ str = String(buffer);
+ }
+};
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/List.hpp b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/List.hpp
new file mode 100644
index 0000000..0b53033
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/List.hpp
@@ -0,0 +1,345 @@
+#ifndef HEADER_LIST
+#define HEADER_LIST
+
+/*! \file List.hpp
+ * \brief Stores declaration and definition of templated class List
+ */
+
+#include <istream>
+#include <ostream>
+
+/*! \brief Templated class that stores an array of elements in dynamic memory
+ *
+ * \warning findIndex(), contain(), read(), write(), operator<<() and operator>>() require the type to have defined a couple of functions.
+ * \sa findIndex()
+ * \sa contain()
+ * \sa read()
+ * \sa write()
+ * \sa operator<<()
+ * \sa operator>>()
+ */
+template <typename T>
+class List {
+protected:
+ T* elements;
+ unsigned length;
+ unsigned count;
+
+ void resize();
+ void free();
+ void copyFrom(const List& other);
+
+public:
+ //! Copies all elements in given array
+ List(const T* elements, unsigned elementsCount);
+ //! Adds an element
+ void add(const T& element);
+ //! Inserts element at given index
+ void insertAt(const T& element, unsigned index);
+ //! Removes element at index
+ bool removeAt(unsigned index);
+ //! Finds the index of element
+ unsigned findIndex(const T& element) const;
+ bool contain(const T& element) const;
+ //! Returns reference to element at index
+ T& operator[](unsigned index);
+ //! Returns constant reference to element at index
+ const T& operator[](unsigned index) const;
+
+ //! Appends elements from other list
+ List<T>& operator+=(const List<T> other);
+
+ //! Reads from stream
+ std::istream& read(std::istream& istr);
+ //! Writes to stream
+ std::ostream& write(std::ostream& ostr) const;
+
+ //! Returns the length
+ unsigned get_length() const;
+ //! Returns the count
+ unsigned get_count() const;
+
+ List();
+ List& operator=(const List& other);
+ List(const List& other);
+ ~List();
+
+ List(List&& other);
+ List& operator=(List&& other);
+};
+
+//! Reads List from stream with >> operator
+template <typename T>
+std::istream& operator>>(std::istream& istr, List<T>& obj);
+//! Writes List to stream with << operator
+template <typename T>
+std::ostream& operator<<(std::ostream& ostr, const List<T>& obj);
+
+/* Private */
+
+template <typename T>
+void List<T>::resize() {
+ length = (length == 0) ? 8 : length << 1;
+ T* temp = new T[length];
+ for (int i = 0; i < count; i++)
+ temp[i] = elements[i];
+ delete[] elements;
+ elements = temp;
+}
+
+template <typename T>
+void List<T>::free() {
+ delete[] elements;
+}
+
+template <typename T>
+void List<T>::copyFrom(const List& other) {
+ elements = new T[other.length];
+ for (int i = 0; i < other.count; i++)
+ elements[i] = other.elements[i];
+ length = other.length;
+ count = other.count;
+}
+
+/* Public */
+
+template <typename T>
+List<T>::List(const T* elements, unsigned elementsCount) {
+ length = 8;
+ count = elementsCount;
+ this->elements = new T[length];
+ for (unsigned i = 0; i < count; i++)
+ this->elements[i] = elements[i];
+}
+
+/*!
+ * Resizes internal array if there is no space for additional elements.
+ */
+template <typename T>
+void List<T>::add(const T& element) {
+ if (length == count) resize();
+
+ elements[count++] = element;
+}
+
+/*!
+ * If index is after the last element, the element is just added.
+ * Otherwise all elements after the index are shifted right and element is put in place.
+ *
+ * Resizes internal array if there is no space for additional elements.
+ */
+template <typename T>
+void List<T>::insertAt(const T& element, unsigned index) {
+ if (index >= count) {
+ add(element);
+ return;
+ }
+ if (length == count) resize();
+
+ for (unsigned i = count; i > index; i--)
+ elements[i] = elements[i - 1];
+
+ elements[index] = element;
+ count++;
+}
+
+/*! \returns Wether element could be removed
+ *
+ * If index is after that of the last element, nothing is done and false is returned.
+ * Otherwise elements after index are shifted right and count is reduced.
+ */
+template <typename T>
+bool List<T>::removeAt(unsigned index) {
+ if (index >= count) return false;
+
+ for (int i = index; i < count; i++)
+ elements[i] = elements[i + 1];
+ count--;
+ return true;
+}
+
+/*! \returns Index of element. If element isn't found, returns the count of element.
+ *
+ * \warning The function depends on the type having a function "compare" defined, which takes two elements and returns a number <0 if elem1 < elem2, >0 if elem1 > elem2, 0 if elem1 == elem2
+ * \note Searching is done linearly
+ */
+template <typename T>
+unsigned List<T>::findIndex(const T& element) const {
+ unsigned ind = 0;
+ while (ind < count && elements[ind].compare(element) != 0)
+ ind++;
+ return ind;
+}
+
+/*! \returns Whether the element is contained in the current List
+ *
+ * \warning Function depends on findIndex(), which means the same "compare" function must be defined in the type
+ * \sa findIndex()
+ */
+template <typename T>
+bool List<T>::contain(const T& element) const {
+ return findIndex(element) < count;
+}
+
+template <typename T>
+T& List<T>::operator[](unsigned index) {
+ return elements[index];
+}
+
+template <typename T>
+const T& List<T>::operator[](unsigned index) const {
+ return elements[index];
+}
+
+template <typename T>
+List<T>& List<T>::operator+=(const List<T> other) {
+ for (unsigned i = 0; i < other.length; i++)
+ add(other[i]);
+ return *this;
+}
+
+/*! \param istr An input stream
+ *
+ * Directly reads bytes from stream (calls read() function).
+ * Any stored values are deleted and replaced with those from the stream.
+ *
+ * \warning The function depends on the type having a function "read" defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ifstream
+ */
+template <typename T>
+std::istream& List<T>::read(std::istream& istr) {
+ istr.read((char*)&length, sizeof(length));
+ istr.read((char*)&count, sizeof(count));
+
+ delete[] elements;
+ elements = new T[length];
+
+ for (int i = 0; i < count; i++)
+ elements[i].read(istr);
+
+ return istr;
+}
+
+/*! \param ostr An output stream
+ *
+ * Directly writes bytes to stream (calls write() function).
+ *
+ * \warning The function depends on the type having a function "write" defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ofstream
+ */
+template <typename T>
+std::ostream& List<T>::write(std::ostream& ostr) const {
+ ostr.write((const char*)&length, sizeof(length));
+ ostr.write((const char*)&count, sizeof(count));
+
+ for (int i = 0; i < count; i++)
+ elements[i].write(ostr);
+
+ return ostr;
+}
+
+/*!
+ * Length is the size of the underlying array (allocated memory).
+ */
+template <typename T>
+unsigned List<T>::get_length() const {
+ return length;
+}
+
+/*!
+ * Count is the amount of elements that are stored.
+ */
+template <typename T>
+unsigned List<T>::get_count() const {
+ return count;
+}
+
+// Rule of 4
+
+template <typename T>
+List<T>::List() : List(nullptr, 0) {}
+
+template <typename T>
+List<T>& List<T>::operator=(const List& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <typename T>
+List<T>::List(const List& other) {
+ copyFrom(other);
+}
+
+template <typename T>
+List<T>::~List() {
+ free();
+}
+
+// Move semantics
+
+template <typename T>
+List<T>::List(List&& other) {
+ length = other.length;
+ count = other.count;
+ elements = other.elements;
+ other.elements = nullptr;
+}
+
+template <typename T>
+List<T>& List<T>::operator=(List&& other) {
+ if (this != &other) {
+ free();
+ length = other.length;
+ count = other.count;
+ elements = other.elements;
+ other.elements = nullptr;
+ }
+ return *this;
+}
+
+/* Outside of class */
+
+/*!
+ * Uses the stream's >> operator to read and parse the elements.
+ * The first item in the stream should be the count.
+ *
+ * \warning The function depends on the type having the operator >> defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
+ * \note Best used with std::cin or text std::ifstream
+ */
+template <typename T>
+std::istream& operator>>(std::istream& istr, List<T>& obj) {
+ List<T> newObj;
+ unsigned count;
+ istr >> count;
+
+ T temp;
+ for (int i = 0; i < count; i++) {
+ istr >> temp;
+ obj.add(temp);
+ }
+
+ return istr;
+}
+
+/*!
+ * Uses the stream's << operator to write the count and then all objects.
+ *
+ * \warning The function depends on the type having the operator << defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
+ * \note Best used with std::cout or text std::ofstream
+ */
+template <typename T>
+std::ostream& operator<<(std::ostream& ostr, const List<T>& obj) {
+ ostr << obj.get_count() << std::endl;
+ for (int i = 0; i < obj.get_count(); i++)
+ ostr << obj[i];
+
+ return ostr;
+}
+
+
+#endif
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Location.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Location.h
new file mode 100644
index 0000000..1b5f78e
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Location.h
@@ -0,0 +1,4 @@
+struct Location {
+ int x;
+ int y;
+};
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/RadioButton.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/RadioButton.h
new file mode 100644
index 0000000..bab00e1
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/RadioButton.h
@@ -0,0 +1,6 @@
+#include "Control.h"
+#include <iostream>
+
+class RadioButton : public Control {
+
+};
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Size.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Size.h
new file mode 100644
index 0000000..fef6cc7
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/Size.h
@@ -0,0 +1,9 @@
+#ifndef H_SIZE
+#define H_SIZE
+
+struct Size {
+ int height;
+ int width;
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/String.cpp b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/String.cpp
new file mode 100644
index 0000000..9d61999
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/String.cpp
@@ -0,0 +1,169 @@
+#include "String.h"
+#include <cstring>
+
+/* Private */
+
+void String::free() {
+ delete[] str;
+}
+
+void String::copyFrom(const String& other) {
+ str = new char[other.length + 1];
+ strncpy(str, other.str, other.length + 1);
+ length = other.length;
+}
+
+/* Public */
+
+/*!
+ * Allocated with length + 1 size, so there is a terminating zero at the end.
+ */
+String::String(unsigned length) {
+ this->length = length;
+ str = new char[length + 1];
+ str[length] = '\0';
+}
+
+/*! \params str C-style string
+ */
+String::String(const char* str) {
+ length = strlen(str);
+ this->str = new char[length + 1]; // capture terminating zero
+ strncpy(this->str, str, length + 1);
+}
+
+const char* String::get_cstr() const {
+ return str;
+}
+
+unsigned String::get_length() const {
+ return length;
+}
+
+char& String::operator[](unsigned index) {
+ return str[index];
+}
+
+/*! \params str C-style string
+ */
+String& String::operator+=(const char* str) {
+ unsigned strLen = strlen(str);
+ char* newStr = new char[length + strLen + 1];
+ strncpy(newStr, this->str, length + 1);
+ strncat(newStr, str, strLen + 1);
+
+ delete[] this->str;
+ this->str = newStr;
+ length += strLen;
+
+ return *this;
+}
+
+/*!
+ * Converts the number to a C-style string and then uses += to append it.
+ */
+String& String::operator+=(unsigned number) {
+ unsigned tempLen = 0;
+ for (unsigned tmp = number; tmp > 0 || tempLen == 0; tmp /= 10)
+ tempLen++;
+
+ char* tmp = new char[tempLen + 1];
+ tmp[tempLen] = '\0';
+ for (unsigned i = tempLen - 1; i < tempLen; i--, number /= 10)
+ tmp[i] = number % 10 + '0';
+
+ *this += tmp;
+ delete[] tmp;
+ return *this;
+}
+
+String::String() : String("") {}
+
+String::String(const String& other) {
+ copyFrom(other);
+}
+
+String& String::operator=(const String& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+String::~String() {
+ free();
+}
+
+String::String(String&& other) {
+ str = other.str;
+ other.str = nullptr;
+ length = other.length;
+}
+
+String& String::operator=(String&& other) {
+ if (this != &other) {
+ delete[] str;
+ str = other.str;
+ other.str = nullptr;
+ length = other.length;
+ }
+ return *this;
+}
+
+/*! \param istr An input stream
+ *
+ * Directly reads bytes from stream (calls read() function).
+ * First reads the string length, then the underlying C-style string (including terminating zero).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ifstream
+ */
+void String::read(std::istream& istr) {
+ istr.read((char*)&length, sizeof(length));
+ delete[] str;
+ str = new char[length + 1];
+ istr.read(str, sizeof(char) * (length + 1));
+}
+
+/*! \param ostr An output stream
+ *
+ * Directly writes bytes to stream (calls write() function).
+ * First writes the string length, then the underlying C-style string (including terminating zero).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ofstream
+ */
+void String::write(std::ostream& ostr) const {
+ ostr.write((char*)&length, sizeof(length));
+ ostr.write(str, sizeof(char) * (length + 1));
+}
+
+/*! \params other C-style string
+ * \returns strcmp between underlying C-style string and "other"
+ */
+int String::compare(const String& other) const {
+ return strcmp(str, other.str);
+}
+
+/*!
+ * Uses the stream's getline function to read the data.
+ *
+ * \warning It takes at most 1024 characters from the stream!
+ * \note Best used with std::cin or text std::ifstream
+ */
+std::istream& operator>>(std::istream& istr, String& event) {
+ istr.getline(event.str, 1024);
+ event.length = strlen(event.str);
+
+ return istr;
+}
+
+/*!
+ * Uses the stream's << operator to write the underlying C-style string
+ *
+ * \note Best used with std::cout or text std::ofstream
+ */
+std::ostream& operator<<(std::ostream& ostr, const String& event) {
+ return ostr << event.str;
+}
diff --git a/Year 1/Object-oriented Programming/13-Practice-31.05.2022/String.h b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/String.h
new file mode 100644
index 0000000..84fbe9c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/13-Practice-31.05.2022/String.h
@@ -0,0 +1,56 @@
+#ifndef HEADER_STRING
+#define HEADER_STRING
+
+/*! \file String.h
+ * \brief Stores declaration of class String
+ */
+
+#include <istream>
+#include <ostream>
+
+class String {
+ char* str;
+ unsigned length;
+
+ void free();
+ void copyFrom(const String& other);
+
+public:
+ //! The underlying array is allocated with length (+ 1) size
+ String(unsigned length);
+ //! Copies str
+ String(const char* str);
+ //! Returns internal C-style string
+ const char* get_cstr() const;
+ //! Returns number of characters in string
+ unsigned get_length() const;
+ //! Returns a character reference (from the underlying C-style string) at the given index
+ char& operator[](unsigned index);
+
+ //! Appends a C-style string
+ String& operator+=(const char* str);
+ //! Appends a number
+ String& operator+=(unsigned number);
+
+ String();
+ String(const String& other);
+ String& operator=(const String& other);
+ ~String();
+
+ String(String&& other);
+ String& operator=(String&& other);
+
+ //! Reads String from stream
+ void read(std::istream& istr);
+ //! Writes String to stream
+ void write(std::ostream& ostr) const;
+ //! Compares two strings
+ int compare(const String& other) const;
+
+ //! Reads String from stream with >> operator
+ friend std::istream& operator>>(std::istream& istr, String& event);
+ //! Writes String to stream with << operator
+ friend std::ostream& operator<<(std::ostream& ostr, const String& event);
+};
+
+#endif