Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Exam 2 / ex1
diff options
context:
space:
mode:
Commita82a54f081ce561e6bc6e9f82869f8b40a47e660 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-25 19:59:16 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-25 19:59:16 +0300
Filesa0cf9fc2e5f954456bdec52db780e1778c9808e6 /Year 1/Object-oriented Programming/Exam 2/ex1
uni-projects-a82a54f081ce561e6bc6e9f82869f8b40a47e660.tar
uni-projects-a82a54f081ce561e6bc6e9f82869f8b40a47e660.tar.gz
uni-projects-a82a54f081ce561e6bc6e9f82869f8b40a47e660.zip
Parent71c57cde42e99301aeb079be4ef368ff155f99c5 (diff)
Added my work from today's exam
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.cpp
new file mode 100644
index 0000000..d065206
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.cpp
@@ -0,0 +1,15 @@
+#include "AirTravel.h"
+
+AirTravel::AirTravel() : AirTravel("", 0) {}
+
+AirTravel::AirTravel(const char* flightNumber, unsigned duration) : flightNumber(flightNumber) {
+ this->duration = duration;
+}
+
+String AirTravel::getFlightNumber() const {
+ return flightNumber;
+}
+
+unsigned AirTravel::getDuration() const {
+ return duration;
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.h b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.h
new file mode 100644
index 0000000..c7f24ca
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/AirTravel.h
@@ -0,0 +1,18 @@
+#ifndef HEADER_AIRTRAVEL
+#define HEADER_AIRTRAVEL
+
+#include "String.h"
+
+class AirTravel {
+ String flightNumber;
+ unsigned duration;
+
+public:
+ AirTravel();
+ AirTravel(const char* flightNumber, unsigned duration);
+
+ String getFlightNumber() const;
+ unsigned getDuration() const;
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.cpp
new file mode 100644
index 0000000..6632f00
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.cpp
@@ -0,0 +1,22 @@
+#include "CarTravel.h"
+
+CarTravel::CarTravel() : CarTravel(RegNumb(), 0, 0) {}
+
+CarTravel::CarTravel(RegNumb regNumber, unsigned distance, unsigned duration) {
+ this->regNumb = regNumber;
+ this->distance = distance;
+ this->duration = duration;
+}
+
+RegNumb CarTravel::getRegNumb() const {
+ return regNumb;
+}
+
+unsigned CarTravel::getDistance() const {
+ return distance;
+}
+
+unsigned CarTravel::getDuration() const {
+ return duration;
+}
+
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.h b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.h
new file mode 100644
index 0000000..b483d66
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CarTravel.h
@@ -0,0 +1,20 @@
+#ifndef HEADER_CARTRAVEL
+#define HEADER_CARTRAVEL
+
+#include "RegistryNumber.h"
+
+class CarTravel {
+ RegNumb regNumb;
+ unsigned distance;
+ unsigned duration;
+
+public:
+ CarTravel();
+ CarTravel(RegNumb regNumber, unsigned distance, unsigned duration);
+
+ RegNumb getRegNumb() const;
+ unsigned getDistance() const;
+ unsigned getDuration() const;
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp
new file mode 100644
index 0000000..dbe315b
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp
@@ -0,0 +1,66 @@
+#include "CombinedTrip.h"
+#include <cstring>
+#include <iostream>
+
+void CombinedTrip::free() {
+ for (unsigned i = 0; i < destinationsCount; i++)
+ delete[] destinations[i];
+ delete[] destinations;
+}
+
+void CombinedTrip::copyFrom(const CombinedTrip& other) {
+ destinationsCount = other.destinationsCount;
+ destinations = new char*[destinationsCount];
+ for (unsigned length = 0, i = 0; i < destinationsCount; i++) {
+ length = strlen(other.destinations[i]);
+ destinations[i] = new char[length + 1];
+ strncpy(destinations[i], other.destinations[i], length + 1);
+ }
+}
+
+CombinedTrip::CombinedTrip(RegNumb carRegNumb, unsigned carTravelDistance, unsigned carTravelDuration, const char* flightNumber, unsigned flightDuration, const char** destinations, unsigned destinationsCount, unsigned freeTime) : carTravel(carRegNumb, carTravelDistance, carTravelDuration), airTravel(flightNumber, flightDuration) {
+ if (destinations == nullptr) {
+ destinations = nullptr;
+ destinationsCount = freeTime = 0;
+ return;
+ }
+
+ this->destinationsCount = destinationsCount;
+ this->destinations = new char*[destinationsCount];
+ for (unsigned length = 0, i = 0; i < destinationsCount; i++) {
+ length = strlen(destinations[i]);
+ this->destinations[i] = new char[length + 1];
+ strncpy(this->destinations[i], destinations[i], length + 1);
+ }
+ this->freeTime = freeTime;
+}
+
+unsigned CombinedTrip::getDuration() const {
+ return carTravel.getDuration() + freeTime + airTravel.getDuration();
+}
+
+void CombinedTrip::print() {
+ std::cout << "Air Travel || Flight number: " << airTravel.getFlightNumber() << " | Duration: " << airTravel.getDuration() << std::endl;
+ std::cout << "Car Travel || Registry number: " << carTravel.getRegNumb() << carTravel.getRegNumb() << " | Durationt: " << carTravel.getDuration() << " | Distance: " << carTravel.getDistance();
+ std::cout << "Free time: " << freeTime << std::endl << "Destinations: ";
+ for (unsigned i = 0; i < destinationsCount; i++)
+ std::cout << destinations[i] << std::endl;
+}
+
+CombinedTrip::CombinedTrip() : CombinedTrip(RegNumb(), 0, 0, "", 0, nullptr, 0, 0) {}
+
+CombinedTrip::CombinedTrip(const CombinedTrip& ct) {
+ copyFrom(ct);
+}
+
+CombinedTrip& CombinedTrip::operator=(const CombinedTrip& ct) {
+ if (this != &ct) {
+ free();
+ copyFrom(ct);
+ }
+ return *this;
+}
+
+CombinedTrip::~CombinedTrip() {
+ free();
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.h b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.h
new file mode 100644
index 0000000..77f7090
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.h
@@ -0,0 +1,29 @@
+#ifndef HEADER_COMBINEDTRIP
+#define HEADER_COMBINEDTRIP
+
+#include "String.h"
+#include "CarTravel.h"
+#include "AirTravel.h"
+
+class CombinedTrip {
+ CarTravel carTravel;
+ AirTravel airTravel;
+ char** destinations; // цитирам "представя се чрез динамичен масив от низове от тип char*"
+ unsigned destinationsCount;
+ unsigned freeTime;
+
+ void free();
+ void copyFrom(const CombinedTrip& other);
+public:
+ CombinedTrip(RegNumb carRegNumb, unsigned carTravelDistance, unsigned carTravelDuration, const char* flightNumber, unsigned flightDuration, const char** destinations, unsigned destinationsCount, unsigned freeTime);
+
+ unsigned getDuration() const;
+ void print();
+
+ CombinedTrip();
+ CombinedTrip(const CombinedTrip& ct);
+ CombinedTrip& operator=(const CombinedTrip& ct);
+ ~CombinedTrip();
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.cpp
new file mode 100644
index 0000000..0d6a3bd
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.cpp
@@ -0,0 +1,15 @@
+#include "RegistryNumber.h"
+#include <ostream>
+
+RegNumb::RegNumb() : RegNumb(0, 0, 0, 0) {}
+
+RegNumb::RegNumb(int regNumber1, int regNumber2, int regNumber3, int regNumber4) {
+ regNumb[0] = regNumber1;
+ regNumb[1] = regNumber2;
+ regNumb[2] = regNumber3;
+ regNumb[3] = regNumber4;
+}
+
+std::ostream& operator<<(std::ostream& ostr, const RegNumb& regNumb) {
+ return ostr << regNumb.regNumb[0] << regNumb.regNumb[1] << regNumb.regNumb[2] << regNumb.regNumb[3];
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.h b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.h
new file mode 100644
index 0000000..5101551
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/RegistryNumber.h
@@ -0,0 +1,17 @@
+#ifndef HEADER_REGNUM
+#define HEADER_REGNUM
+
+#include <ostream>
+
+class RegNumb {
+ int regNumb[4];
+
+public:
+ RegNumb();
+ RegNumb(int regNumber1, int regNumber2, int regNumber3, int regNumber4);
+
+ friend std::ostream& operator<<(std::ostream& ostr, const RegNumb& regNumb);
+};
+
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp
new file mode 100644
index 0000000..693e638
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/String.cpp
@@ -0,0 +1,40 @@
+#include "String.h"
+#include <cstring>
+
+void String::copyFrom(const String& other) {
+ length = other.length;
+ str = new char[length + 1];
+ strncpy(str, other.str, length + 1);
+}
+
+void String::free() {
+ delete[] str;
+}
+
+String::String(const char* str) {
+ length = strlen(str);
+ this->str = new char[length + 1];
+ strncpy(this->str, str, length + 1);
+}
+
+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();
+}
+
+std::ostream& operator<<(std::ostream& ostr, const String& str) {
+ return ostr << str.str;
+}
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/String.h b/Year 1/Object-oriented Programming/Exam 2/ex1/String.h
new file mode 100644
index 0000000..997093c
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/String.h
@@ -0,0 +1,23 @@
+#ifndef HEADER_STRING
+#define HEADER_STRING
+
+#include <ostream>
+
+class String {
+ char* str;
+ unsigned length;
+
+ void copyFrom(const String& other);
+ void free();
+public:
+ String(const char* str);
+
+ String();
+ String(const String& other);
+ String& operator=(const String& other);
+ ~String();
+
+ friend std::ostream& operator<<(std::ostream& ostr, const String& str);
+};
+
+#endif
diff --git a/Year 1/Object-oriented Programming/Exam 2/ex1/main.cpp b/Year 1/Object-oriented Programming/Exam 2/ex1/main.cpp
new file mode 100644
index 0000000..0ea5920
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Exam 2/ex1/main.cpp
@@ -0,0 +1,20 @@
+#include "CombinedTrip.h"
+#include <cstring>
+
+int main() {
+ while (1) {
+ char** dest = new char*[3];
+ for (unsigned i = 0; i < 3; i++)
+ dest[i] = new char[2];
+ strcpy(dest[0], "A");
+ strcpy(dest[1],"B");
+ strcpy(dest[2], "C");
+
+ CombinedTrip ct(RegNumb(1, 2, 3, 4), 1, 5, "ASKDJHS", 35, (const char**)dest, 3, 57);
+ ct.print();
+
+ for (unsigned i = 0; i < 3; i++)
+ delete[] dest[i];
+ delete[] dest;
+ }
+}