编写矩阵类--封装矩阵的运算(加、减、乘、转置、...)

2024年11月21日 23:15
有1个网友回答
网友(1):

class CMatrix
{
public:
CMatrix(){m_pData = 0;}
CMatrix(int rows, int cols) : m_iRows(rows), m_iCols(cols)
{
pData = new double[m_iRows * m_iCols];
//Generate data here
}
~CMatrix()
{
if(pData) delete []pData;
}
CMatrix& operator=(CMatrix& other);
double* operator[](int row)
{
return m_pData + row*m_iCols;
}
CMatrix& operator+(CMatrix& mat);
CMatrix& operator-(CMatrix& mat);
CMatrix& operator*(CMatrix& mat);
CMatrix& operator*(double c);
CMatrix& operator/(double c);
CMatrix& trans();//transpose
CMatrix& inv();//inverse
double det();//determinant

private:
int m_iRows, m_iCols;
double *m_pData;
}