Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Square Matrix Project (C++)

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.


📦 Project Structure

  • 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.

🧩 Class and Functions

Class: SquareMat

Represents an n x n square matrix of real numbers, stored in a dynamic 2D array.

🔹 Constructors & Destructor

  • SquareMat(int size) – Creates an n x n zero-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).

🔹 Arithmetic Operators

  • +, - – 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).

🔹 Increment & Decrement

  • ++, -- – Pre and post increment/decrement (add or subtract 1 from all elements).

🔹 Access & Utility

  • operator[](int row) – Access row by index, supports mat[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.

🔹 Comparison Operators

  • ==, != – Compare matrices by sum of elements.
  • <, >, <=, >= – Compare matrices by sum of elements.

🔹 Compound Assignment Operators

  • +=, -=, *=, /=, %= – Perform the operation in place.

🔹 I/O Operators

  • << – Prints the matrix in a human-readable form.
  • friend operators allow operations like scalar * matrix.

▶️ Running the Project

Requirements

  • g++ compiler supporting C++11 or higher
  • valgrind (optional, for memory leak checks)
  • doctest (header-only framework for unit testing)

Build and Run

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

Author:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages