#include <iostream>
#include <stdexcept>
class Matrix {
private:
int rows;
int cols;
double** data;
public:
// 构造函数
Matrix(int r, int c) : rows(r), cols(c) {
data = new double* [rows];
for (int i = 0; i < rows; ++i) {
data[i] = new double[cols];
for (int j = 0; j < cols; ++j) {
data[i][j] = 0.0;
}
}
}
// 析构函数
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] data[i];
}
delete[] data;
}
// += 运算符重载
Matrix& operator+=(const Matrix& other) {
if (rows != other.rows || cols != other.cols) {
throw std::invalid_argument("Matrices must have the same dimensions");
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
data[i][j] += other.data[i][j];
}
}
return *this;
}
};