面向对象程序设计上机练习十一(运算符重载)

来源:互联网 发布:网络免费发布信息平台 编辑:程序博客网 时间:2024/05/18 01:49

Problem Description

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

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

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

2 3 4
3 5 3
4 3 1
5 4 3
Example Output

6 6 5
8 9 6

#include <iostream>#include <iomanip>#include <stdio.h>using namespace std;class Complex{private:    int a[2][3];public:    Complex()    {        a[2][3] = 0;    }    void input()    {        for(int i = 0; i < 2; i++)        {            for(int j = 0; j < 3; j++)            {                cin>>a[i][j];            }        }    }    Complex operator +(Complex &b)    {        Complex c;        for(int i = 0; i < 2; i++)        {            for(int j = 0; j < 3; j++)            {                c.a[i][j] = a[i][j] + b.a[i][j];            }        }        return c;    }    void show()    {        int i, j;        for(i = 0; i < 2; i++)        {            for(j = 0; j < 2; j++)            {                cout<<a[i][j]<<" ";            }            cout<<a[i][j]<<endl;        }    }};int main(){    Complex a, b, c;    a.input();    b.input();    c = a+b;    c.show();    return 0;}
阅读全文
0 0
原创粉丝点击