sdut 在机器上面向对象编程练习11(运算符重载)

在机器上面向对象编程练习11(运算符重载)

Time Limit: 1000MS Memory limit: 65536K

标题叙述性说明

有两个矩阵a和b,均为2行3列,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c=a+b。

输入

第1、2行是矩阵a的值,数据以空格分开。
第3、4行是矩阵b的值。数据以空格分开。

输出

2个矩阵a、b之和,以行列形式显示。

演示样例输入

2 3 4
3 5 3
4 3 1
5 4 3

演示样例输出

6 6 5
8 9 6

来源

#include <iostream>

using namespace std;

class position
{
private:
    int x1,x2,x3;
    int y1,y2,y3;

public:
    position(int a=0,int b=0,int c=0,int d=0,int e=0,int f=0)
    {
        x1=a;
        x2=b;
        x3=c;
        y1=d;
        y2=e;
        y3=f;
    }

    position operator +(position & r);
    friend ostream&operator <<(ostream&, position&);
};

position position :: operator +(position & r)
{
    position p;
    p.x1=x1+r.x1;
    p.x2=x2+r.x2;
    p.x3=x3+r.x3;
    p.y1=y1+r.y1;
    p.y2=y2+r.y2;
    p.y3=y3+r.y3;
    return p;
}

ostream & operator << (ostream & output, position &r)
{
    output<<r.x1<<" "<<r.x2<<" "<<r.x3<<endl;
    output<<r.y1<<" "<<r.y2<<" "<<r.y3;
    return output;
}

int main()
{
    int a,b,c,d,e,f;
    cin>>a>>b>>c>>d>>e>>f;
    position r1(a,b,c,d,e,f);
    
    cin>>a>>b>>c>>d>>e>>f;
    position r2(a,b,c,d,e,f);
    
    position r3;
    r3=r1+r2;
    cout<<r3<<endl;
    return 0;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4874388.html