Creating a matrix class overloading the +, -, * and << operators

For my data structures class, we have been given the assignment to create a matrix overloading the +, -, * and << operators.

I cannot get any output. It compiles correctly, and no error messages are being printed in the buffer.

When I debug, this is the output:

'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Users\Ragan E. Lake\Documents\Visual Studio 2015\Projects\Assignment2rl_test3\Debug\Assignment2rl_test3.exe'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\kernel32.dll'
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded.
'Assignment2rl_test3.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded.
The thread 0x8e0 has exited with code 0 (0x0).
The thread 0x29a0 has exited with code 0 (0x0).
The thread 0x2810 has exited with code 0 (0x0).
The program '[13088] Assignment2rl_test3.exe' has exited with code 0 (0x0).


Researching online suggested creating a breakpoint, which I did to no effect.


If someone could look at the code and tell me where I'm off, I would appreciate it:

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<fstream>
#include<string>

using namespace std;

class matrixType
{
public:
matrixType(); //default constructor
matrixType operator*(matrixType& right); //function to overload + operator
matrixType operator+(const matrixType &right); //function to overload + operator
matrixType operator-(const matrixType &right); //function to overload - operator
matrixType& operator=(const matrixType& right); // function to overload = operator
matrixType(int numberOfRows, int numberOfColumns); //constructor
matrixType(const matrixType &otherMatrix); //copy constructor
~matrixType(); //destructor


protected:
int **board; //pointer to matrix
friend ostream& operator<<(ostream&, const matrixType&); //function to overload <<operator
int numberOfRows; //variable to store rows of the matrix
int numberOfColumns; //variable to store columns of the matrix
};


//Default constructor
matrixType::matrixType()
{
//Initializes each variable to 1
numberOfRows = 1;
numberOfColumns = 1;

//Creates matrix with random numbers.
board = new int *[numberOfRows];
for (int i = 0; i < numberOfRows; i++)
{
board[i] = new int[numberOfColumns];
for (int j = 0; j < numberOfColumns; j++)
{
board[i][j] = rand() % 100;
}
}
}

matrixType::matrixType(int numberOfRows, int numberOfColumns)
{
//If the numbers entered for Rows and Columns is 0 or less than zero, set them to 1.
//if (numberOfRows <= 0)
//{
// cout << "Matrix must contain at least one row." << endl;
// numberOfRows = 1;
//}
//Pointer to number of rows.
board = new int * [numberOfRows];

//Creates matrix and populates cells with random numbers.
for (int i = 0; i < numberOfRows; i++)
{
board[i] = new int[numberOfColumns];
//if (numberOfColumns <= 0)
//{
// cout << "The matrix must contain at least 1 column. Setting matrix to have 1 column." << endl;
// numberOfColumns = 1;
//}
for (int j = 0; j < numberOfColumns; j++)
{
board[i][j] = rand() % 100;
}
}
}

//Destructor
matrixType::~matrixType()
{
for (int i = 0; i < numberOfRows; i++)
delete[] board[i];
delete[] board;
numberOfRows = numberOfColumns = 0;
}

//Copy constructor
matrixType::matrixType(const matrixType& otherMatrix)
{
numberOfRows = otherMatrix.numberOfRows;
numberOfColumns = otherMatrix.numberOfColumns;

board = new int *[numberOfRows];
for (int i=0; i < numberOfRows; i++)
{
for (int j = 0; j < numberOfColumns; j++)
{
board[i][j] = otherMatrix.board[i][j];
}
}
}


//Function to overload the + operator
matrixType matrixType::operator+(const matrixType &matrix)
{

if (this->numberOfRows != matrix.numberOfRows || this->numberOfColumns != matrix.numberOfColumns)
{
cout << "Addition could not take place because the matrices do not have the same number of rows and columns.";
return *this;
}

matrixType result(numberOfRows, numberOfColumns);

for (int i = 0; i < numberOfRows; i++)
{
for (int j = 0; j < numberOfColumns; j++)
{
result.board[i][j] = this->board[i][j] + matrix.board[i][j];
}
}
return result;
}

//Function to overload the - operator
matrixType matrixType::operator-(const matrixType &matrix)
{
if (this->numberOfRows != matrix.numberOfRows || this->numberOfColumns != matrix.numberOfColumns)
{
cout << "Subtraction could not take place because the matrices do not have the same number of rows and columns.";
return *this;
}
matrixType result (numberOfRows, numberOfColumns);
//newMatrix.numberOfRows = matrix.numberOfRows;
//newMatrix.numberOfColumns = matrix.numberOfColumns;

for (int i = 0; i < numberOfRows; i++)
{
for (int j = 0; j < numberOfColumns; j++)
{
result.board[i][j] = this->board[i][j] - matrix.board[i][j];
}
}
return result;
}

//Function to overload the * operator
matrixType matrixType::operator * (matrixType& matrix)
{
if (this->numberOfColumns != matrix.numberOfRows)
{
cout << "Multiplication could not take place because number of columns of first matrix and number of rows in second matrix are different.";
return *this;
}
matrixType result(this->numberOfRows, matrix.numberOfColumns);
for (int i = 0; i < this->numberOfRows; i++)
{
for (int j = 0; j < matrix.numberOfColumns; j++)
{
for (int k = 0; k < this->numberOfColumns; k++)
{
result.board[i][j] += this->board[i][k] * matrix.board[k][j];

}
}
}
return result;
}

//Function to overload the = operator
matrixType& matrixType::operator=(const matrixType& rightMatrix)
{
int rows = rightMatrix.numberOfRows;
int columns = rightMatrix.numberOfColumns;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
board[i][j] = rightMatrix.board[i][j];
}
}
return *this;
}

//Function to overload the << operator
ostream& operator<<(ostream& osObject, const matrixType &matrixA)
{
for (int i = 0; i < matrixA.numberOfRows; ++i)
{
for (int j = 0; j < matrixA.numberOfColumns; ++j)
{
osObject << " " << (matrixA.board[i][j]);

}
}
return osObject << endl;
}


int main() {
int rows1, columns1, rows2, columns2;
cout << "Enter the number of rows and columns for the first matrix: " << endl;
cin >> rows1 >> columns1;
cout << "You entered " << rows1 << " rows" << " and " << columns1 << " columns for the first matrix." << endl;
matrixType matrix1(rows1, columns1); //Creates first matrix.
cout << matrix1;

cout << "\nEnter the number of rows and columns for the second matrix: (The dimensions of the second matrix must match the dimensions of the first." << endl;
cin >> rows2 >> columns2;
cout << "You entered " << rows2 << " rows" << " and " << columns2 << " columns for the second matrix." << endl;
matrixType matrix2(rows2, columns2); //Creates second matrix.
cout << matrix2;

matrixType matrixSum = matrix1 + matrix2;

cout << "The sum of the first matrix and the second matrix is " << matrixSum << endl;

matrixType matrixDifference = matrix1 - matrix2;

cout << "The difference between the first matrix and the second matrix is " << matrixDifference << endl;

matrixType matrixProduct = matrix1 * matrix2;

cout << "The difference between the first matrix and the second matrix is " << matrixProduct << endl;

string buffer;
cout << buffer;
system("pause");
}

Thanks!

Ragan
Last edited on
I cannot get any output.

That's not a lot to go off of. When I run your code I get the following output:

Enter the number of rows and columns for the first matrix: 
2
2
You entered 2 rows and 2 columns for the first matrix.

Enter the number of rows and columns for the second matrix: 
2
2
You entered 2 rows and 2 columns for the second matrix.


 
Exit code: 0 (normal program termination)


What output were you expecting?
In your two-parameter constructor, you forgot to assign the value of numberOfRows and numberOfColumns to this->numberOfRows and this->numberOfColumns.

Basically, your member variables were never initialized in the two-parameter constructor and you tried to use the uninitialized numberOfRows and numberOfColumns in operator<<

That's the first problem I saw. I didn't look at the rest of your code because the code posted without code tags is hard to read and I don't want to waste good 5 minutes of my time just to copy your code and fix indentation before I can even try to find out what is wrong.

Fix the problem I mentioned first and come back if you still have problems.
Last edited on
That worked. Thanks!
Topic archived. No new replies allowed.