C++定义一个m行n列的矩阵类

2024-11-28 21:45:34
推荐回答(1个)
回答(1):

class matrix
{
public:
matrix():r(0), p(0), j(NULL) {}
matrix(int m,int n):r(m),p(n)
{
j = new int*[m];
for(int i = 0; i < m; i++)
{
j[i] = new int[n];
}
}
~matrix()
{
for(int i = 0; i < r; i++)
{
delete[] j[i];
}
delete[] j;
}
matrix(matrix& a) : r(a.r), p(a.p)
{
j = new int*[r];
for(int i = 0; i < r; i++)
{
j[i] = new int[p];
for(int k = 0; k < p; k++)
{
j[i][k] = a.j[i][k];
}
}
}
private:
int** j;
const int r;
const int p;
};