Assigning an array to a derived object
Question
I am trying to write a Matrix Class for a projects. I seperated the square matrix as a derived class. But when I try to perform the minor operation for the determinant, I can't assign the matrix members to the my derived class objects. How can I perform these operation? Actually, I tried a lot, I couldn't make a success. Here is the code;
using namespace std;
class Matrix
{
friend ostream &operator<<(ostream &, const Matrix &);
friend istream &operator>>(istream &, const Matrix &);
private:
int num_col;
int num_row;
int **mits;
public:
Matrix(const int=0 ,const int=0);
Matrix (const Matrix &right);
~Matrix();
int DisplayMenu();
void gotoxy(int x, int y);
int getRow();
int getCol();
int** getMatrix();
virtual void print();
Matrix operator+(const Matrix&);
Matrix operator-(const Matrix&);
Matrix operator*(const Matrix&);
Matrix transMatrix(const Matrix&);
Matrix &operator=(const Matrix&);
};
#endif
# include <iostream>
# include "Matrix.h"
#include <windows.h>
# include <string>
using namespace std;
class SquareMatrix :public Matrix
{
int **sMits;
public:
SquareMatrix(const int=0 ,const int=0);
~SquareMatrix();
virtual void print();
SquareMatrix minor( const int,const int,const int);
int deter( SquareMatrix &mat);
/*SquareMatrix operator*(const SquareMatrix &);
virtual void print(const Matrix &);*/
};
SquareMatrix::SquareMatrix(const int row ,const int col)
:Matrix(row,col)
{
}
SquareMatrix::~SquareMatrix()
{
}
int SquareMatrix::deter(SquareMatrix &a)
{
SquareMatrix M;
int d = 0; // value of the determinant
int rows =a.getRow();
int cols =a.getCol();
sMits=a.getMatrix();
// this is a square matrix
if (rows == 1)
{
// this is a 1 x 1 matrix
d = sMits[0][0];
}
else if (rows == 2)
{
// this is a 2 x 2 matrix
// the determinant of [a11,a12;a21,a22] is det = a11*a22-a21*a12
d = sMits[0][0]* sMits[1][1] - sMits[1][0]* sMits[0][1];
}
else
{
rows--;
cols--;
for (int c = 0; c <= cols; c++)
{
M= minor(c,rows,cols);
d += (c%2 + c%2 - 1) * sMits[0][c] * deter(M);
}
}
return d;
}
SquareMatrix SquareMatrix::minor(const int col,const int rows,const int cols)
{
//int rows =Matrix::getRow();
//int cols =Matrix::getCol();
int **Mits=Matrix::getMatrix();
SquareMatrix min(rows , cols);
cout<<min<<endl;
for (int r = 1; r <= rows ; r++)
{
for (int c = 0; c <= cols ; c++)
{
if (c!=(col))
{
if (c<col)
{
min.sMits[r-1][c] = Mits[r][c];
}
else if (c>col)
{
min.sMits[r-1][c-1] = Mits[r][c];
}
}
}
}
return min;
}
void SquareMatrix::print()
{
Matrix::print();
}
Solution
Lots of problems with this code, but I think the particular problem is that min is a SquareMatrix, and the SquareMatrix constructor calls the Matrix constructor, which presumably initialises Matrix::mits, but I don't see SquareMatrix::sMits being initialised by anything.
Anyway, this line
min.sMits[r-1][c] = Mits[r][c];
accesses SquareMatrix::sMits in min, but SquareMatrix::sMits is uninitialised, which would cause a segfault/bad access exception. You probably meant to access Matrix::mits, but are unable to because Matrix::mits is private, and SquareMatrix only inherits public Matrix.
If you want to access SquareMatrix::sMits in min, you need to populate sMits somehow, or give protected access to mits, and use that instead. i.e.
class Matrix
{
protected:
int** mits;
and then
min.mits[r-1][c] = Mits[r][c];