C++中Matrix类创建及指定成员函数定义实现(面向初学者)
C++ Matrix类实现(面向初学者)
以下是针对需求编写的Matrix类完整代码,包含所有指定成员函数的实现,代码注释清晰,适合初学者理解:
#include <iostream> using namespace std; class Matrix { private: int rows; // 矩阵行数 int cols; // 矩阵列数 int** data; // 动态二维数组存储矩阵元素 // 辅助函数:释放已分配的内存 void freeMemory() { if (data != nullptr) { for (int i = 0; i < rows; i++) { delete[] data[i]; } delete[] data; data = nullptr; } rows = 0; cols = 0; } // 辅助函数:分配内存并初始化为0 void allocateMemory(int r, int c) { freeMemory(); rows = r; cols = c; data = new int*[rows]; for (int i = 0; i < rows; i++) { data[i] = new int[cols](); // 括号确保元素初始化为0 } } public: // 默认构造函数 Matrix() : rows(0), cols(0), data(nullptr) {} // 带参数的构造函数 Matrix(int r, int c) { allocateMemory(r, c); } // 析构函数:防止内存泄漏 ~Matrix() { freeMemory(); } // 拷贝构造函数 Matrix(const Matrix& other) { allocateMemory(other.rows, other.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { data[i][j] = other.data[i][j]; } } } // 拷贝赋值运算符 Matrix& operator=(const Matrix& other) { if (this != &other) { allocateMemory(other.rows, other.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { data[i][j] = other.data[i][j]; } } } return *this; } // 要求实现的成员函数声明 void set(); void set(int val); void set(int r, int c); void set(int r, int c, int val); void set(const Matrix& other); void print() const; int isSqrMatrix() const; int isNullMatrix() const; int isUnitMatrix() const; int isIdentityMatrix() const; int isSymeticMatrix() const; int Equals(const Matrix other) const; };
成员函数具体实现
1. void set()
手动输入矩阵的行数、列数和所有元素值
void Matrix::set() { int r, c; cout << "输入矩阵行数: "; cin >> r; cout << "输入矩阵列数: "; cin >> c; allocateMemory(r, c); cout << "输入矩阵元素(按行依次输入):" << endl; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cin >> data[i][j]; } } }
2. void set(int val)
将矩阵所有元素设置为指定值(若矩阵未初始化,默认创建1x1矩阵)
void Matrix::set(int val) { if (rows == 0 || cols == 0) { allocateMemory(1, 1); } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { data[i][j] = val; } } }
3. void set(int r, int c)
创建r行c列的空矩阵(所有元素初始化为0)
void Matrix::set(int r, int c) { allocateMemory(r, c); }
4. void set(int r, int c, int val)
创建r行c列的矩阵,并将所有元素设置为指定值val
void Matrix::set(int r, int c, int val) { allocateMemory(r, c); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { data[i][j] = val; } } }
5. void set(const Matrix& other)
将当前矩阵复制为传入矩阵的副本
void Matrix::set(const Matrix& other) { *this = other; // 直接调用已实现的拷贝赋值运算符 }
6. void print() const
格式化打印矩阵内容
void Matrix::print() const { if (rows == 0 || cols == 0) { cout << "空矩阵" << endl; return; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cout << data[i][j] << "\t"; } cout << endl; } }
7. int isSqrMatrix() const
判断是否为方阵(行数等于列数),是返回1,否则返回0
int Matrix::isSqrMatrix() const { return (rows == cols) ? 1 : 0; }
8. int isNullMatrix() const
判断是否为零矩阵(所有元素均为0),是返回1,否则返回0
int Matrix::isNullMatrix() const { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (data[i][j] != 0) { return 0; } } } return 1; }
9. int isUnitMatrix() const
判断是否为单位矩阵(方阵,主对角线为1,其余为0),是返回1,否则返回0
int Matrix::isUnitMatrix() const { if (!isSqrMatrix()) { return 0; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if ((i == j && data[i][j] != 1) || (i != j && data[i][j] != 0)) { return 0; } } } return 1; }
10. int isIdentityMatrix() const
与单位矩阵为同一概念,直接复用isUnitMatrix的实现
int Matrix::isIdentityMatrix() const { return isUnitMatrix(); }
11. int isSymeticMatrix() const
判断是否为对称矩阵(方阵,且data[i][j] == data[j][i]),是返回1,否则返回0
int Matrix::isSymeticMatrix() const { if (!isSqrMatrix()) { return 0; } for (int i = 0; i < rows; i++) { for (int j = i + 1; j < cols; j++) { if (data[i][j] != data[j][i]) { return 0; } } } return 1; }
12. int Equals(const Matrix other) const
判断当前矩阵与传入矩阵是否完全相同(行数、列数、所有元素均一致),是返回1,否则返回0
int Matrix::Equals(const Matrix other) const { if (rows != other.rows || cols != other.cols) { return 0; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (data[i][j] != other.data[i][j]) { return 0; } } } return 1; }
测试示例
int main() { Matrix m1; m1.set(); // 手动输入矩阵 cout << "\n你输入的矩阵是:" << endl; m1.print(); Matrix m2; m2.set(3, 3, 0); // 创建3x3零矩阵 cout << "\n3x3零矩阵:" << endl; m2.print(); cout << "是否为零矩阵: " << (m2.isNullMatrix() ? "是" : "否") << endl; Matrix m3; m3.set(3,3); m3.set(0,0,1); m3.set(1,1,1); m3.set(2,2,1); cout << "\n3x3单位矩阵:" << endl; m3.print(); cout << "是否为单位矩阵: " << (m3.isUnitMatrix() ? "是" : "否") << endl; Matrix m4; m4.set(m3); // 拷贝m3的内容 cout << "\n拷贝后的矩阵是否和m3相等: " << (m4.Equals(m3) ? "是" : "否") << endl; return 0; }
内容的提问来源于stack exchange,提问作者Mohit
相关产品推荐
相关产品推荐

