1273 面向对象程序设计上机练习十一(运算符重载)
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
有两个矩阵a和b,均为2行3列,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c=a+b。
Input
第1、2行是矩阵a的值,数据以空格分开。
第3、4行是矩阵b的值,数据以空格分开。
Output
2个矩阵a、b之和,以行列形式显示。
Example Input
2 3 43 5 35 4 34 3 1
Example Output
6 6 58 9 6
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int a = 0, int b = 0 ,int c =0)//构造函数
{
x = a;
y = b;
z = c;
}
void get_in()
{
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void put_out()
{
cout << x <<' '<< y <<' '<< z <<endl;
}
Complex operator + (Complex &t)
{
Complex ha;
ha.x = t.x + x;
ha.y = t.y + y;
ha.z = t.z + z;
return ha;
}
private:
int x,y,z;
int a,b,c;
};
int main()
{
Complex c1,c2,c3,c4,c5,c6;
c1.get_in();
c2.get_in();
c3.get_in();
c4.get_in();
c5 = c1 + c3;
c6 = c2 + c4;
c5.put_out();
c6.put_out();
return 0;
}