diff options
feat(Week09): Add solutions
2 files changed, 124 insertions, 0 deletions
| -rw-r--r-- | Week09/Exercise01/Locomotive.cpp | 81 | ||||
| -rw-r--r-- | Week09/Exercise01/Locomotive.h | 43 |
diff --git a/Week09/Exercise01/Locomotive.cpp b/Week09/Exercise01/Locomotive.cpp new file mode 100644 index 0000000..2475126 --- /dev/null +++ b/Week09/Exercise01/Locomotive.cpp @@ -0,0 +1,81 @@ +#include "Locomotive.h" +#include <cstring> + +unsigned Locomotive::GetID() const { + return ID; +} + +void Locomotive::SetID(unsigned ID) { + this->ID = ID; +} + +const char* Locomotive::GetManufacturer() const { + return manufacturer; +} + +void Locomotive::SetManufacturer(const char manufacturer[MAX_LEN]) { + strncpy(this->manufacturer, manufacturer, MAX_LEN); +} + +const char* Locomotive::GetModel() const { + return model; +} + +void Locomotive::SetModel(const char model[MAX_LEN]) { + strncpy(this->model, model, MAX_LEN); +} + +int Locomotive::GetYearOfManufacture() const { + return yearOfManufacture; +} + +void Locomotive::SetYearOfManufacture(int yearOfManufacture) { + this->yearOfManufacture = yearOfManufacture; +} + +double Locomotive::GetLength() const { + return length; +} + +void Locomotive::SetLength(double length) { + this->length = length; +} + +unsigned short Locomotive::GetCyllinders() const { + return cyllinders; +} + +void Locomotive::SetCyllinders(unsigned short cyllinders) { + this->cyllinders = cyllinders; +} + +bool Locomotive::GetSupportsMultipleWorking() const { + return supportsMultipleWorking; +} + +void Locomotive::SetSupportsMultipleWorking(bool supportsMultipleWorking) { + this->supportsMultipleWorking = supportsMultipleWorking; +} + +// Тази функция не е нужно да бъде приятелска, можеше просто да използваме +// константните селектори. +std::ofstream& operator<<(std::ofstream& ostr, const Locomotive& loc) { + ostr << "Train #" << loc.ID << std::endl + << "+ Model: " << loc.model << std::endl + << "+ Manufacturer: " << loc.manufacturer << std::endl + << "+ Year of manufacture: " << loc.yearOfManufacture << std::endl + << "- Length: " << loc.length << std::endl + << "- Engine cyllinders: " << loc.cyllinders << std::endl + << "- Multiple working: " << (loc.supportsMultipleWorking ? "Yes" : "No") << std::endl; + return ostr; +} + +// Тазу функция не е нужно да бъде приятелска, можеше просто да използваме +// мутаторите. +std::ifstream& operator>>(std::ifstream& istr, Locomotive& loc) { + istr >> loc.ID; + istr.getline(loc.model, MAX_LEN); + istr.getline(loc.manufacturer, MAX_LEN); + istr >> loc.yearOfManufacture >> loc.length >> loc.cyllinders >> loc.supportsMultipleWorking; + return istr; +} diff --git a/Week09/Exercise01/Locomotive.h b/Week09/Exercise01/Locomotive.h new file mode 100644 index 0000000..45e9f7b --- /dev/null +++ b/Week09/Exercise01/Locomotive.h @@ -0,0 +1,43 @@ +#ifndef _LOCOMOTIVE +#define _LOCOMOTIVE + +#include <fstream> + +const unsigned MAX_LEN = 1024; + +class Locomotive { + unsigned ID; + char manufacturer[MAX_LEN]; + char model[MAX_LEN]; + int yearOfManufacture; + double length; + unsigned short cyllinders; + bool supportsMultipleWorking; + +public: + unsigned GetID() const; + void SetID(unsigned ID); + + const char* GetManufacturer() const; + void SetManufacturer(const char manufacturer[MAX_LEN]); + + const char* GetModel() const; + void SetModel(const char model[MAX_LEN]); + + int GetYearOfManufacture() const; + void SetYearOfManufacture(int yearOfManufacture); + + double GetLength() const; + void SetLength(double length); + + unsigned short GetCyllinders() const; + void SetCyllinders(unsigned short cyllinders); + + bool GetSupportsMultipleWorking() const; + void SetSupportsMultipleWorking(bool supportsMultipleWorking); + + friend std::ofstream& operator<<(std::ofstream& ostr, const Locomotive& loc); + friend std::ifstream& operator>>(std::ifstream& istr, Locomotive& loc); +}; + +#endif /* _LOCOMOTIVE */ |
