Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
diff options
context:
space:
mode:
Commite4f64cec4b7c85b1e39c221a57bcab4cb1980d7d (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-25 19:59:57 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-25 19:59:57 +0300
Filese379ab71093534a870b39000c50870c38e590944 /Year 1/Object-oriented Programming
uni-projects-e4f64cec4b7c85b1e39c221a57bcab4cb1980d7d.tar
uni-projects-e4f64cec4b7c85b1e39c221a57bcab4cb1980d7d.tar.gz
uni-projects-e4f64cec4b7c85b1e39c221a57bcab4cb1980d7d.zip
Parenta82a54f081ce561e6bc6e9f82869f8b40a47e660 (diff)
Added old work from practicum 11
diff --git a/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Battery.cpp b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Battery.cpp
new file mode 100644
index 0000000..6fcbb76
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Battery.cpp
@@ -0,0 +1,12 @@
+#include "Battery.h"
+
+Battery::Battery() : Battery(0, "") {}
+
+Battery::Battery(unsigned short capacity, const char* batteryID) {
+ this->capacity = capacity;
+ this->batteryID = batteryID;
+}
+
+std::ostream& operator<<(std::ostream& ostr, const Battery& bat) {
+ return ostr << "(" << bat.
+}
diff --git a/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Battery.h b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Battery.h
new file mode 100644
index 0000000..9c30927
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Battery.h
@@ -0,0 +1,15 @@
+#include "CarPart.h"
+#include "String.h"
+#include <istream>
+#include <ostream>
+
+class Battery : CarPart {
+ unsigned short capacity;
+ String batteryID;
+
+public:
+ Battery();
+ Battery(unsigned short capacity, const char* batteryID);
+};
+
+std::ostream& operator<<(std::ostream& ostr, const Battery& bat);
diff --git a/Year 1/Object-oriented Programming/11-Practice-10.05.2022/CarPart.h b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/CarPart.h
new file mode 100644
index 0000000..9b26d88
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/CarPart.h
@@ -0,0 +1,15 @@
+#include "String.h"
+#include <istream>
+#include <ostream>
+
+class CarPart {
+ unsigned id;
+ String manufacturer;
+ String description;
+
+public:
+ CarPart();
+ CarPart(unsigned id, String manufacturer, String description);
+};
+
+std::ostream& operator<<(std::ostream& ostr, const Battery& bat);
diff --git a/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Engine.h b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Engine.h
new file mode 100644
index 0000000..fd188f9
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Engine.h
@@ -0,0 +1,13 @@
+#include "CarPart.h"
+#include <istream>
+#include <ostream>
+
+class Engine : CarPart {
+ unsigned short horsepower;
+
+public:
+ Engine();
+ Engine(unsigned short horsepower);
+};
+
+std::ostream& operator<<(std::ostream& ostr, const Battery& bat);
diff --git a/Year 1/Object-oriented Programming/11-Practice-10.05.2022/String.cpp b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/String.cpp
new file mode 100644
index 0000000..66e9ad4
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/String.cpp
@@ -0,0 +1,167 @@
+#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));
+ 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/11-Practice-10.05.2022/String.h b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/String.h
new file mode 100644
index 0000000..84fbe9c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.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
diff --git a/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Tire.h b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Tire.h
new file mode 100644
index 0000000..6855d04
--- /dev/null
+++ b/Year 1/Object-oriented Programming/11-Practice-10.05.2022/Tire.h
@@ -0,0 +1,15 @@
+#include "CarPart.h"
+#include <istream>
+#include <ostream>
+
+class Tire : CarPart {
+ unsigned short width;
+ unsigned short profile;
+ unsigned short diameter;
+
+public:
+ Tire();
+ Tire(unsigned short width, unsigned short profile, unsigned short diameter);
+};
+
+std::ostream& operator<<(std::ostream& ostr, const Battery& bat);