This project implements a square matrix class (SquareMat) in C++ as part of a systems programming assignment.
The goal is to practice concepts such as operator overloading, friend functions, and the Rule of Three while managing dynamic memory without using the C++ STL containers.
- SquareMat.hpp – Header file containing the class declaration.
- SquareMat.cpp – Implementation of all methods and operators.
- main.cpp – Demonstration of the matrix class in action.
- tests.cpp – Unit tests using the Doctest framework.
- Makefile – Build automation with support for demo, tests, and valgrind.
Represents an n x n square matrix of real numbers, stored in a dynamic 2D array.
SquareMat(int size)– Creates ann x nzero-initialized matrix.SquareMat(const SquareMat& other)– Copy constructor (deep copy).~SquareMat()– Destructor to free allocated memory.SquareMat& operator=(const SquareMat& other)– Copy assignment operator (deep copy).
+,-– Element-wise addition and subtraction.- Unary
-– Negates all elements. *– Matrix × Matrix multiplication, and Matrix × Scalar.%– Element-wise multiplication, or modulo with scalar./– Division by scalar.^– Matrix power (repeated multiplication).
++,--– Pre and post increment/decrement (add or subtract 1 from all elements).
operator[](int row)– Access row by index, supportsmat[i][j].~– Transpose operator.!– Determinant of the matrix.sum()– Returns the sum of all elements.minorMatrix(int row, int col)– Returns the minor matrix used for determinant calculation.
==,!=– Compare matrices by sum of elements.<,>,<=,>=– Compare matrices by sum of elements.
+=,-=,*=,/=,%=– Perform the operation in place.
<<– Prints the matrix in a human-readable form.friendoperators allow operations likescalar * matrix.
- g++ compiler supporting C++11 or higher
- valgrind (optional, for memory leak checks)
- doctest (header-only framework for unit testing)
Use the provided Makefile.
# Run the demo program
make Main
# Run the unit tests
make test
# Run memory check with valgrind
make valgrind
# Clean build files
make clean- Ron Avraham
- ronavraham1999@gmail.com