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

2024-11-21 23:15:23
推荐回答(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;
}