# 编程题：运算符重载---矩阵求和

/*

#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix &,Matrix &);
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,Matrix&);
private:
int mat[2][3];
};
Matrix::Matrix()
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
mat[i][j]=0;
}
istream & operator>>(istream &  input,Matrix & m)
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
input>>m.mat[i][j];
return input;
}
//将程序需要的其他成份写在下面，只提交begin到end部分的代码
//******************** begin ********************
//********************* end ********************
int main()
{
Matrix a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<c<<endl;
return 0;
}

``````1 2 3
4 5 6

7 8 9
0 1 2
``````

``````8 10 12
4 6 8 ``````

``````#include<stdio.h>
int main()
{

int a[2][3],b[2][3],i,j,c[2][3];
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%d ",c[i][j]);
printf("\n");
}
return 0;
}``````

``````
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class person
{
public:
person(){}
person(char na[],char s,int ag):sex(s),age(ag)
{
strcpy(name,na);
}
void output() const;
private:
char name[20];
char sex;
int age;
};
void person::output() const
{
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"age:"<<age<<endl;
}
class soider:virtual public person
{
public:
soider(){}
soider(char na[],char s,int age,int y,int m,int d):person(na,s,age),year(y),month(m),day(d){}
void output() const;
private:
int year,month,day;
};
void soider::output()const
{
person::output();
cout<<"join_time:"<<year<<"-"<<month<<"-"<<day<<endl;
}
class General:public soider
{
public:General(char na[],char s,int age,int y,int m,int d,int ar_y):person(na,s,age),soider(na,s,age,y,m,d),army_year(ar_y){}//填写将军类的构造函数。
void output() const;
private:
int army_year;
};
void General::output() const
{
soider::output();
cout<<"army_years:"<<army_year<<endl;
}
int main()
{
char name[20],sex;
int age,y,m,d,army_years;
cin>>name>>sex>>age;
cin>>y>>m>>d;
cin>>army_years;
General general(name,sex,age,y,m,d,army_years);
general.output();
return 0;
}``````