diff --git a/labs/2/04-operator-overloading-friends-and-more/solutions/Source.cpp b/labs/2/04-operator-overloading-friends-and-more/solutions/Source.cpp new file mode 100644 index 0000000..8534db3 --- /dev/null +++ b/labs/2/04-operator-overloading-friends-and-more/solutions/Source.cpp @@ -0,0 +1,20 @@ +#include +#include "Vector.h" +#include "Virus.h" + +int main() +{ + Vector vec; + + vec += Virus("Corona", 5); + vec += Virus("The flu", 3); + vec += Virus("Spanish flu", 6); + vec += Virus("Corona B", 8); + vec += Virus("Virus Z", 10); + + std::cout << vec << std::endl; + vec.sort(); + std::cout << vec << std::endl; + + return 0; +} diff --git a/labs/2/04-operator-overloading-friends-and-more/solutions/Vector.cpp b/labs/2/04-operator-overloading-friends-and-more/solutions/Vector.cpp new file mode 100644 index 0000000..32d6f2e --- /dev/null +++ b/labs/2/04-operator-overloading-friends-and-more/solutions/Vector.cpp @@ -0,0 +1,164 @@ +#include +#include "Vector.h" + +const int DEFAULT_CAPACITY = 4; + +Vector::Vector(int capacity) + : m_size(0) + , m_capacity(std::max(capacity, DEFAULT_CAPACITY)) + , m_arr(new elem_t[m_capacity]) +{} + + +Vector::Vector(const Vector& other) +{ + copy(other); +} + + +Vector& Vector::operator=(const Vector& other) +{ + if (this != &other) { + clear(); + copy(other); + } + return *this; +} + + +Vector::~Vector() +{ + clear(); +} + + +void Vector::copy(const Vector& other) +{ + m_size = other.m_size; + m_capacity = other.m_capacity; + m_arr = new elem_t[m_capacity]; + for (int i = 0; i < m_size; i++) + m_arr[i] = other.m_arr[i]; +} + + +void Vector::clear() +{ + delete[] m_arr; + m_arr = nullptr; +} + + +int Vector::size() const +{ + return m_size; +} + + +int Vector::capacity() const +{ + return m_capacity; +} + + +elem_t& Vector::at(int index) +{ + return m_arr[index]; +} + + +const elem_t& Vector::at(int index) const +{ + return m_arr[index]; +} + + +void Vector::push_back(const elem_t& element) +{ + if (m_size >= m_capacity) + reserve(2 * m_capacity); + + m_arr[m_size] = element; + ++m_size; +} + + +void Vector::erase(int index) +{ + --m_size; + for (int i = index; i < m_size; i++) + m_arr[i] = m_arr[i + 1]; +} + + +void Vector::reserve(int capacity) +{ + if (capacity > m_capacity) { + m_capacity = capacity; + elem_t* new_arr = new elem_t[capacity]; + + for (int i = 0; i < m_size; i++) + new_arr[i] = m_arr[i]; + + delete[] m_arr; + m_arr = new_arr; + } +} + + +void Vector::sort() +{ + for (int i = 0; i < m_size - 1; i++) { + int maxIndex = i; + + for (int j = i + 1; j < m_size; j++) + if (m_arr[j] > m_arr[maxIndex]) + maxIndex = j; + + if (maxIndex != i) + std::swap(m_arr[i], m_arr[maxIndex]); + } +} + + +elem_t& Vector::operator[](int index) +{ + return at(index); +} + + +const elem_t& Vector::operator[](int index) const +{ + return at(index); +} + + +Vector& Vector::operator+=(const elem_t& elem) +{ + push_back(elem); + return *this; +} + + +Vector& Vector::operator+=(const Vector& other) +{ + reserve(m_size + other.m_size); + for (int i = 0; i < other.m_size; i++) + *this += other[i]; + return *this; +} + + +std::ostream& operator<<(std::ostream& out, const Vector& obj) +{ + out << "[ "; + + if (obj.size() >= 1) + out << obj[0]; + + for (int i = 1; i < obj.size(); i++) + out << ", " << obj[i]; + + out << " ]"; + return out; +} diff --git a/labs/2/04-operator-overloading-friends-and-more/solutions/Vector.h b/labs/2/04-operator-overloading-friends-and-more/solutions/Vector.h new file mode 100644 index 0000000..2c8e826 --- /dev/null +++ b/labs/2/04-operator-overloading-friends-and-more/solutions/Vector.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include "Virus.h" + +typedef Virus elem_t; + +class Vector +{ +public: + // Big four + Vector(int capacity = 0); + Vector(const Vector& other); + Vector& operator=(const Vector& other); + ~Vector(); + + // Methods + int size() const; + int capacity() const; + elem_t& at(int index); + const elem_t& at(int index) const; + void push_back(const elem_t& element); + void erase(int index); + void reserve(int capacity); + void sort(); + + // Operators + elem_t& operator[](int index); + const elem_t& operator[](int index) const; + Vector& operator+=(const elem_t& elem); + Vector& operator+=(const Vector& other); + +private: + // Helper methods used in the big four + void copy(const Vector& other); + void clear(); + + // Data members + int m_size; + int m_capacity; + elem_t* m_arr; +}; + +std::ostream& operator<<(std::ostream& out, const Vector& obj); diff --git a/labs/2/04-operator-overloading-friends-and-more/solutions/Virus.cpp b/labs/2/04-operator-overloading-friends-and-more/solutions/Virus.cpp new file mode 100644 index 0000000..b7bfd8a --- /dev/null +++ b/labs/2/04-operator-overloading-friends-and-more/solutions/Virus.cpp @@ -0,0 +1,91 @@ +#include +#include "Virus.h" + +Virus::Virus(const char* name, double severity) + : severity(severity) + , name(new char[strlen(name) + 1]) +{ + strcpy(this->name, name); +} + + +Virus::Virus(const Virus& other) +{ + copy(other); +} + + +Virus& Virus::operator=(const Virus& other) +{ + if (this != &other) { + clear(); + copy(other); + } + return *this; +} + + +Virus::~Virus() +{ + clear(); +} + + +void Virus::copy(const Virus& other) +{ + severity = other.severity; + name = new char[strlen(other.name) + 1]; + strcpy(name, other.name); +} + + +void Virus::clear() +{ + delete[] name; +} + + +bool Virus::operator<(const Virus& other) const +{ + return severity < other.severity; +} + + +bool Virus::operator>=(const Virus& other) const +{ + return !(*this < other); +} + + +bool Virus::operator>(const Virus& other) const +{ + return severity > other.severity; +} + + +bool Virus::operator<=(const Virus& other) const +{ + return !(*this > other); +} + + +std::ostream& operator<<(std::ostream& out, const Virus& obj) +{ + return out << obj.getName() << " (" << obj.getSeverity() << ")"; +} + + +void Virus::setName(const char* newName) +{ + if (newName != nullptr) { + delete[] name; + name = new char[strlen(newName) + 1]; + strcpy(name, newName); + } +} + + +void Virus::setSeverity(double newSeverity) +{ + severity = newSeverity; +} diff --git a/labs/2/04-operator-overloading-friends-and-more/solutions/Virus.h b/labs/2/04-operator-overloading-friends-and-more/solutions/Virus.h new file mode 100644 index 0000000..df99d4b --- /dev/null +++ b/labs/2/04-operator-overloading-friends-and-more/solutions/Virus.h @@ -0,0 +1,31 @@ +#pragma once +#include + +class Virus +{ +public: + Virus(const char* name = "", double severity = 0); + Virus(const Virus& other); + Virus& operator=(const Virus& other); + ~Virus(); + + bool operator<(const Virus& other) const; + bool operator>=(const Virus& other) const; + bool operator>(const Virus& other) const; + bool operator<=(const Virus& other) const; + + const char* getName() const { return name; } + void setName(const char* newName); + double getSeverity() const { return severity; } + void setSeverity(double newSeverity); + +private: + + void copy(const Virus& other); + void clear(); + + double severity; + char* name; +}; + +std::ostream& operator<<(std::ostream& out, const Virus& obj);