From 88c6f71ec1befeb795dbc28a119abae702038a31 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 2 Oct 2023 22:59:51 +0200 Subject: [PATCH] TD2 Finished. --- td/td2/include/vector.h | 35 ++++++++++++++++++++++++++++++++--- td/td2/src/main.cpp | 29 ++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/td/td2/include/vector.h b/td/td2/include/vector.h index bd2b8e4..e111814 100644 --- a/td/td2/include/vector.h +++ b/td/td2/include/vector.h @@ -19,10 +19,11 @@ public: friend std::ostream& operator<<(std::ostream& stream, const Vector& vec); template friend std::istream& operator>>(std::istream& stream, Vector& vec); - Vector& operator=(const Vector& other); Vector& operator()(const Vector& other); - T operator[](const int indice); + T& operator[](const int indice); + Vector operator+(const Vector& other); + Vector& operator+=(const Vector& other); private: size_t _capacity; @@ -141,7 +142,7 @@ std::istream& operator>>(std::istream& stream, Vector& vec) } template -T Vector::operator[](const int indice) +T& Vector::operator[](const int indice) { if(indice > _size) throw "Indice from the vector is not reachable"; @@ -149,4 +150,32 @@ T Vector::operator[](const int indice) return _values[indice]; } +template +Vector Vector::operator+(const Vector& other) +{ + if(_size != other._size) + throw "Cannot add two vectors with different sizes together"; + else + { + Vector returnVector(_size); + returnVector._size = _size; + for(auto i =0; i < _size; i++) + returnVector._values[i] = _values[i] + other._values[i]; + return returnVector; + } +} + +template +Vector& Vector::operator+=(const Vector& other) +{ + if(_size != other._size) + throw "Cannot add two vectors with different sizes together"; + else + { + for(auto i =0; i < _size; i++) + _values[i] += other._values[i]; + return *this; + } +} + #endif // HEADER_VECTOR \ No newline at end of file diff --git a/td/td2/src/main.cpp b/td/td2/src/main.cpp index 5756cc2..25c306a 100644 --- a/td/td2/src/main.cpp +++ b/td/td2/src/main.cpp @@ -1,21 +1,36 @@ -#include #include "../include/vector.h" +#include int main(void) { Vector intVector; - for(int i=0; i<6; i++) - { + for (int i = 0; i < 6; i++) { intVector.PushBack(i); std::cout << intVector[i] << std::endl; } - try{ - std::cout << intVector[12] << std::endl; + + try { + std::cout << intVector[12] << std::endl; + } catch (const char* expr) { + std::cout << "Error: " << expr << std::endl; } - catch(const char* expr) - { + intVector[2] = 21; + std::cout << intVector << std::endl; + + Vector secondVector(intVector.Size(), 5), tooLargeVector(3, 10); + std::cout << secondVector + intVector << std::endl; + try { + std::cout << tooLargeVector + intVector << std::endl; + } catch (const char* expr) { std::cout << "Error: " << expr << std::endl; } + intVector+=secondVector; + std::cout << intVector << std::endl; + try { + tooLargeVector += intVector; + } catch (const char* expr) { + std::cout << "Error: " << expr << std::endl; + } return 0; } \ No newline at end of file