Operations with matrices – Matrix Class in c ++

The following is the first exercise I did with matrix. Perhaps some of these exercises can serve as a base.

You can read the previous post about vectors, matrices and pointers if you still do not work with them.

I recommend that you try and practice a lot before copying and pasting. If you feel stuck in some problem just try to find a guide to reach the solution.

This matrix class will have 3 attributes: Matrixs, number of rows and number of columns. It has a default constructor, copy constructor and a constructor per parameter that receives the number of rows and columns.

The following functions were implemented:

Generate a matrices dynamically
Fill the matrix from the keyboard:
Fill the matrix randomly:
Print the matrix
Find the greatest element.
Find the smallest element.
Find the fashion.
Exchange rows
Exchange columns
Add another matrix: Add 2 matrix-type objects to return another matrix. Axis: c = a + b.
Subtract another matrix: Same as the sum. Axis: c = a – b
Multiply by another matrix: Axis: c = a * b. The number of rows of a must be equal to the number of columns of b
Multiply by a scalar: Enter a number and all the elements of the matrix are multiplied by that number.
Find transposed matrix: matr [m] [n] its transpose is matr [n] [m]. It is obtained by changing rows by columns. Where the elements of the row m now belong to the column m of the transpose.
Verify if it is symmetric: A matrix is ​​symmetric if it is square (rows = columns) and when its transpose is equal to its original matrix.
Verify if it is identity: It is identity if it has all its null elements except those of the main diagonal that are equal to 1.
NOTE: I have applied some templates to manage the matrix with several types of data (int, float, char, double) and operator overload for the addition, subtraction and multiplication of matrices.

I am just learning to use these 2 important language features (templates and operator overload).


A template is a way for functions, classes, methods to be used with various types of data. Imagine creating a list of data and having to create functions insert, delete, search, concatenate, etc for each type of data. If the methods and the class have the same logic to rewrite code if we can reuse.

Operator overload is a way to perform the same operations that we usually do with common data types with abstract data types. For example, the overload allowed me to add 2 Matrix objects and store the result in another Matrix object, in the form c = a + b

Here is the definition and implementation of the Matrix class.

You may also like...

1 Response

  1. 2017

    […] Previous story Operations with matrices – Matrix Class in c ++ […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!