习题 10.5 在第4题的基础上,重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。
代码:
#include<iostream>
using namespace std;
class Arr
{
public:
void input();
void display();
friend Arr (operator +)(Arr, Arr);
friend ostream& operator << (ostream& , Arr& );
private:
double arr[2][3];
};
void Arr::input() //定义输入数据函数
{
cout << "输入矩阵的值:" << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> arr[i][j];
}
}
}
void Arr::display()
{
cout << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main()
{
Arr a1, a2, a3;
a1.input();
a2.input();
a3 = a1 + a2;
cout << a3;
return 0;
}
Arr operator +(Arr a1,Arr a2)
{
Arr a;