From 7e35b218d94688bee3df716f9f17278a16c26f58 Mon Sep 17 00:00:00 2001 From: Leto Date: Tue, 24 Dec 2024 14:14:04 +0100 Subject: [PATCH] = operator uses new Swap function and added + operator --- main.cpp | 6 +++++- matrices.h | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index b4cc933..3f21ff6 100644 --- a/main.cpp +++ b/main.cpp @@ -7,9 +7,13 @@ int main() Matrix a(3,2); a.Randomize(); + Matrix b = a; a.Print("A"); - a.Transpose().Print("Transposed"); + b.Print("B"); + + Matrix result = a + &b; + result.Print("A + B"); return 0; } \ No newline at end of file diff --git a/matrices.h b/matrices.h index 20bcd9a..7b043d6 100644 --- a/matrices.h +++ b/matrices.h @@ -14,6 +14,8 @@ class Matrix{ inline void Set(float); + inline Matrix Swap(const Matrix*); + inline void Multiply(float); inline Matrix Multiply(const Matrix*); @@ -40,8 +42,11 @@ class Matrix{ }; Matrix Matrix::operator=(const Matrix* other){ - this->values = other->values; - return *this; + return this->Swap(other); +} + +Matrix Matrix::operator+(const Matrix* other){ + return this->Add(other); } // Constructs a zero matrix @@ -59,6 +64,11 @@ Matrix::Matrix(const Matrix* other){ this->values = other->values; } +Matrix Matrix::Swap(const Matrix* other){ + this->values = other->values; + return *this; +} + void Matrix::Hadamard(const Matrix* other){ // Matrices need to be the same size assertm(this->values.size() == other->values.size() &&