sdut 物件導向程式設計上機練習十一(運算子多載)

來源:互聯網
上載者:User
物件導向程式設計上機練習十一(運算子多載) Time Limit: 1000MS Memory limit: 65536K 題目描述 有兩個矩陣a和b,均為2行3列,求兩個矩陣之和。重載運算子“+”,使之能用於矩陣相加。如:c=a+b。 輸入 第1、2行是矩陣a的值,資料以空格分開。
第3、4行是矩陣b的值,資料以空格分開。 輸出 2個矩陣a、b之和,以行列形式顯示。 樣本輸入
2 3 43 5 34 3 15 4 3
樣本輸出
6 6 58 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.