diff options
Added my work from today's exam
1 files changed, 66 insertions, 0 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/Exam 2/ex1/CombinedTrip.cpp | 66 |
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(); +} |
