Task
完善Matrix类的定义,实现简单的Matrix运算,类的定义,main函数已给出,你需要编写Matrix.h文件实现具体函数,记得包含类的定义。
Detail
class Matrix
//定义Matrix类
{
public:
Matrix(); //默认构造函数
~Matrix(); //析构函数
Matrix(const Matrix &); //拷贝构造函数
Matrix(int row, int col); //含参数构造函数
Matrix operator+(const Matrix &)const; //重载运算符“+”
Matrix& operator=(const Matrix &); //重载运算符“=”
Matrix transpose()const; //矩阵的转置
void display()const; //输出数据函数
private:
int row; //矩阵的行
int col; //矩阵的列
int** mat; //用于储存矩阵
};
Hint
类的构造函数与析构函数,运算符重载,new与delete
Sample Input 1
2 3
1 2 3
1 2 3
1 2 3
1 2 3
Sample Output 1
Matrix a:
1 2 3
1 2 3
Matrix b:
1 2 3
1 2 3
Matrix c = Matrix a + Matrix b :
2 4 6
2 4 6
Matrix a transpose to Matrix d:
1 1
2 2
3 3
Sample Input 2
2 2
1 2
3 4
4 3
2 1
Sample Ouput 2
Matrix a:
1 2
3 4
Matrix b:
4 3
2 1
Matrix c = Matrix a + Matrix b :
5 5
5 5
Matrix a transpose to Matrix d:
1 3
2 4
Matrix.cpp
#include <iostream>
#include"Matrix.h"
using namespace std;
int main() {
int row, col;
cout << "input the row and the col for Matrix a, b" << endl;
cin >> row >> col;
Matrix a(row, col), b(row, col), c(a), d;
cout << endl << "Matrix a:" << endl;
a.display();
cout << endl << "Matrix b:" << endl;
b.display();
c = a + b;//用重载运算符“+”实现两个矩阵相加
cout << endl << "Matrix c = Matrix a + Matrix b :" << endl;
c.display();
cout << endl << "Matrix a transpose to Matrix d:" << endl;
d = a.transpose();
d.display();
return 0;
}
Matrix.h
#include <iostream>
using namespace std;
class Matrix
//定义Matrix类
{
public:
Matrix(); //构造函数
~Matrix(); //析构函数
Matrix(const Matrix &); //拷贝构造函数
Matrix(int row, int col); //默认构造函数
Matrix operator+(const Matrix &)const; //重载运算符“+”
Matrix operator=(const Matrix &); //重载运算符“=”
Matrix transpose()const; //矩阵的转置
void display()const; //输出数据函数
private:
int row; //矩阵的行
int col; //矩阵的列
int** mat; //用于储存矩阵
};
Matrix::Matrix() {
row = 0;
col = 0;
mat = NULL;
}
Matrix::~Matrix() {
if(mat != NULL){
for (int i = 0; i < row; i++) {
delete[]mat[i];
}
delete[]mat;
mat = NULL;
}
}
Matrix::Matrix(const Matrix& a) {
row = a.row;
col = a.col;
mat = new int*[row];
for (int i = 0; i < row; i++) {
mat[i] = new int[col];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
mat[i][j] = a.mat[i][j];
}
}
}
Matrix::Matrix(int row, int col)
//定义构造函数
{
this->row = row;
this->col = col;
mat = new int*[row];
for (int i = 0; i < row; i++) {
mat[i] = new int[col];
}
for (int i = 0; i<row; i++)
for (int j = 0; j<col; j++)
cin >> mat[i][j];
}
Matrix Matrix::operator+(const Matrix &b)const
//定义重载运算符“+”函数
{
Matrix c(b);
for (int i = 0; i<b.row; i++)
for (int j = 0; j<b.col; j++)
{
c.mat[i][j] = mat[i][j] + b.mat[i][j];
}
return c;
}
Matrix Matrix::operator=(const Matrix& a)
{
row = a.row;
col = a.col;
if (mat == NULL) {
mat = new int*[row];
for (int i = 0; i < row; i++) {
mat[i] = new int[col];
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
mat[i][j] = a.mat[i][j];
}
}
return *this;
}
Matrix Matrix::transpose()const {
Matrix d;
d.row = col;
d.col = row;
d.mat = new int*[col];
for (int i = 0; i < col; i++) {
d.mat[i] = new int[row];
}
for (int j = 0; j < col; j++)
for (int i = 0; i < row; i++)
{
d.mat[j][i] = mat[i][j];
}
return d;
}
void Matrix::display()const
//定义输出数据函数
{
for (int i = 0; i<row; i++) {
for (int j = 0; j<col; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}