概述
用类来实现矩阵,定义一个矩阵的类Matrix
- 属性包括
- 矩阵大小,用
row
,col
(行、列)来表示 - 存贮矩阵的数组指针
dataptr
,根据矩阵大小动态申请 - 矩阵计数器
matNum
,存储现有矩阵的个数
- 矩阵大小,用
- 矩阵类的方法包括
- 构造函数,参数是矩阵大小,需要动态申请存贮矩阵的数组
- 析构函数,需要释放矩阵的数组指针
- 拷贝构造函数,需要申请和复制数组
- 输入,可以从
cin
中输入矩阵元素 - 输出,将矩阵格式化输出到
cout
- 矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵类,但必须矩阵大小相同
- 矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵类,但必须矩阵大小相同
- 对
+
,-
,=
,<<
,>>
等运算符的重载函数
知识要点
- 内存管理
- 重载
- 友元
实现
#pragma once
#include <iostream>
#include <iomanip>
class Matrix {
private:
int row;
int col;
int** dataptr;
static int matNum;
public:
Matrix(int row_, int col_);
Matrix(const Matrix& mat_);
~Matrix();
static int getNum();
Matrix add(const Matrix& mat_);
Matrix sub(const Matrix& mat_);
Matrix operator+(const Matrix& mat_);
Matrix operator-(const Matrix& mat_);
Matrix operator=(const Matrix& mat_);
friend std::istream& operator>>(std::istream& is_, Matrix& mat_);
friend std::ostream& operator<<(std::ostream& os_, Matrix& mat_);
};
#include "Matrix.h"
int Matrix::matNum{0};
Matrix::Matrix(int row_, int col_)
: row(row_), col(col_) {
dataptr = new int* [row] {nullptr};
for (int i = 0; i < row; i++) {
dataptr[i] = new int[col] {0};
}
matNum++;
}
Matrix::Matrix(const Matrix& mat_)
: row(mat_.row), col(mat_.col) {
dataptr = new int* [row] {nullptr};
for (int i = 0; i < row; i++) {
dataptr[i] = new int[col] {0};
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
dataptr[i][j] = mat_.dataptr[i][j];
}
}
matNum++;
}
Matrix::~Matrix() {
for (int i = 0; i < row; i++) {
delete[] dataptr[i];
}
delete[] dataptr;
matNum--;
}
int Matrix::getNum() {
return matNum;
}
Matrix Matrix::add(const Matrix& mat_) {
if (row != mat_.row || col != mat_.col) {
std::cout << "Error!" << std::endl;
exit(0);
}
Matrix result{row, col};
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.dataptr[i][j] = dataptr[i][j] + mat_.dataptr[i][j];
}
}
return result;
}
Matrix Matrix::sub(const Matrix& mat_) {
if (row != mat_.row || col != mat_.col) {
std::cout << "Error!" << std::endl;
exit(0);
}
Matrix result{row, col};
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.dataptr[i][j] = dataptr[i][j] - mat_.dataptr[i][j];
}
}
return result;
}
Matrix Matrix::operator+(const Matrix& mat_) {
return add(mat_);
}
Matrix Matrix::operator-(const Matrix& mat_) {
return sub(mat_);
}
Matrix Matrix::operator=(const Matrix& mat_) {
if (this == &mat_) {
return *this;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
dataptr[i][j] = mat_.dataptr[i][j];
}
}
return *this;
}
std::istream& operator>>(std::istream& is_, Matrix& mat_) {
for (int i = 0; i < mat_.row; i++) {
for (int j = 0; j < mat_.col; j++) {
is_ >> mat_.dataptr[i][j];
}
}
return is_;
}
std::ostream& operator<<(std::ostream& os_, Matrix& mat_) {
for (int i = 0; i < mat_.row; i++) {
for (int j = 0; j < mat_.col; j++) {
os_ << std::setw(6) << mat_.dataptr[i][j] << ' ';
}
os_ << std::endl;
}
return os_;
}
测试
#include "Matrix.h"
using namespace std;
int main() {
Matrix m1{2, 3}, m2{2, 3};
cout << "Input m1: " << endl;
cin >> m1;
cout << "m1 = " << endl;
cout << m1;
cout << "Input m2: " << endl;
cin >> m2;
cout << "m2 = " << endl;
cout << m2;
Matrix m3 = m1;
cout << "m3 = m1, m3 = " << endl;
cout << m3;
Matrix m4 = m1 + m2;
cout << "m4 = m1 + m2, m4 = " << endl;
cout << m4;
Matrix m5 = m1 - m2;
cout << "m5 = m1 - m2, m5 = " << endl;
cout << m5;
Matrix* m6 = new Matrix{2, 3};
cout << "Input m6: " << endl;
cin >> *m6;
cout << "*m6 = " << endl;
cout << *m6;
Matrix m7 = *m6;
cout << "m7 = *m6, m7 = " << endl;
cout << m7;
delete m6;
cout << "m7 = *m6(deleted), m7 = " << endl;
cout << m7;
m1 = m2 = m7;
cout << "m1 = m2 = m7" << endl;
cout << "m1 = " << endl;
cout << m1;
cout << "m2 = " << endl;
cout << m2;
cout << "MatrixNum = " << Matrix::getNum() << endl;
return 0;
}
Input m1:
1 2 3
4 5 6
m1 =
1 2 3
4 5 6
Input m2:
4 5 6
7 8 9
m2 =
4 5 6
7 8 9
m3 = m1, m3 =
1 2 3
4 5 6
m4 = m1 + m2, m4 =
5 7 9
11 13 15
m5 = m1 - m2, m5 =
-3 -3 -3
-3 -3 -3
Input m6:
1 1 1
1 1 1
*m6 =
1 1 1
1 1 1
m7 = *m6, m7 =
1 1 1
1 1 1
m7 = *m6(deleted), m7 =
1 1 1
1 1 1
m1 = m2 = m7
m1 =
1 1 1
1 1 1
m2 =
1 1 1
1 1 1
MatrixNum = 6