根据main函数中矩阵对象的定义与使用,定义相关的矩阵类Array,并利用运算符重载的方法实现矩阵的加法与输入输出操作。(为简化问题,矩阵中元素为2位以内整数,要求矩阵按照行列的格式输出,每个元素占3位宽度)
代码实现:
#include
#include
using namespace std;
class Array{
private:
int mat[2][3];
public:
Array operator +(Array b){
Array rs;
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)rs.mat[i][j] = mat[i][j]+b.mat[i][j];
}
return rs;
}
friend istream& operator >>(istream& in,Array &a);
friend ostream& operator <<(ostream& out,Array &a);
};
istream& operator >>(istream& in,Array &a){
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)in>>a.mat[i][j];
}
return in;
}
ostream& operator <<(ostream& out,Array &a){
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cout<>arr1;
cin>>arr2;
cout<