#include<iostream>
using namespace std;
class Martrix
{
public:
Martrix() {}
void input();
friend Martrix operator+(Martrix&, Martrix&);
void display();
private:
int mtx[2][3];
};
void Martrix::input()
{
cout << "请输入矩阵:" << endl;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
cin >> mtx[i][j];
}
Martrix operator+(Martrix& m1, Martrix& m2)
{
Martrix m;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
m.mtx[i][j] = m1.mtx[i][j] + m2.mtx[i][j];
return m;
}
void Martrix::display()
{
cout << "新矩阵:" << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
cout << mtx[i][j] << " ";
cout << endl;
}
}
int main()
{
Martrix m1, m2, m3;
m1.input();
m2.input();
m3 = m1 + m2;
m3.display();
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/20e98cda30d81554654f0dc8ce700b4c.png)