Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Project 1 / src / Models / Ticket.cpp
diff options
context:
space:
mode:
Commit24bdf39e6c21ab846f6c86803761d78a094e05c0 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-26 20:31:26 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-26 20:31:26 +0300
Files1b2077b088098cc7ae6afc946b10a4d680110294 /Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp
uni-projects-24bdf39e6c21ab846f6c86803761d78a094e05c0.tar
uni-projects-24bdf39e6c21ab846f6c86803761d78a094e05c0.tar.gz
uni-projects-24bdf39e6c21ab846f6c86803761d78a094e05c0.zip
Parentcbe944835cc0951a946d01f034b4d28ff9874a4e (diff)
Added files from project 1 (mirror)
1 files changed, 71 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp71
diff --git a/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp b/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp
new file mode 100644
index 0000000..bc8ef84
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Project 1/src/Models/Ticket.cpp
@@ -0,0 +1,71 @@
+#include "Ticket.h"
+
+Ticket::Ticket() : Ticket(0, 0) {}
+
+Ticket::Ticket(unsigned row, unsigned seat) {
+ this->row = row;
+ this->seat = seat;
+}
+
+unsigned Ticket::get_row() const {
+ return row;
+}
+
+unsigned Ticket::get_seat() const {
+ return seat;
+}
+
+/*! \param istr An input stream
+ *
+ * Directly reads bytes from stream (calls read() function).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ifstream
+ */
+void Ticket::read(std::istream& istr) {
+ istr.read((char*)&row, sizeof(row));
+ istr.read((char*)&seat, sizeof(seat));
+}
+
+/*! \param ostr An output stream
+ *
+ * Directly writes bytes to stream (calls write() function).
+ *
+ * \remark Doesn't alter the stream in any other way.
+ * \note Best used with binary ofstream
+ */
+void Ticket::write(std::ostream& ostr) const {
+ ostr.write((const char*)&row, sizeof(row));
+ ostr.write((const char*)&seat, sizeof(seat));
+}
+
+/*! \return If rows are equal, -1 if seat < other.seat, 1 if seat > other.seat, 0 if seat == other.seat, else the same, but for rows
+ */
+int Ticket::compare(const Ticket& other) const {
+ if (row == other.row)
+ return (seat > other.seat) - (seat < other.seat);
+ return (row > other.row) - (row < other.row);
+}
+
+/* Outside of class */
+
+/*!
+ * Uses the stream's >> operator to read and parse row and seat.
+ *
+ * \note Best used with std::cin or text std::ifstream
+ */
+std::istream& operator>>(std::istream& istr, Ticket& ticket) {
+ unsigned row, seat;
+ istr >> row >> seat;
+ ticket = Ticket(row, seat);
+ return istr;
+}
+
+/*!
+ * Uses the stream's << operator to write row and seat, separated by a space.
+ *
+ * \note Best used with std::cout or text std::ofstream
+ */
+std::ostream& operator<<(std::ostream& ostr, const Ticket& ticket) {
+ return ostr << ticket.get_row() << " " << ticket.get_seat();
+}