实现两个矩阵做加法运算
#include<iostream.h>
class mix
{
public:
mix();
friend mix operator+(mix &c1,mix &c2);
void input();
void display();
private:
int m[2][3];
};
mix::mix()
{
int i,j;
for (i=0;i<2;i++)
{
for (j=0;j<3;j++)
m[i][j] =0;
}
}
mix operator+(mix &c1,mix &c2)
{
mix c3;
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
c3.m[i][j] = c1.m[i][j] + c2.m[i][j];
}
return c3;
}
void mix::input()
{
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
cin >> m[i][j];
}
}
void mix::display()
{
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
cout << m[i][j];
cout << endl;
}
}
int main()
{
mix c1,c2,c3;
c1.input();
c2.input();
cout << "c1=" << endl;
c1.display();
cout << "c2=" << endl;
c2.display();
c3 = c1 + c2;
cout << "c3=" << endl;
c3.display();
return 0;
}
//注:不能使用 #include <iostream>头文件和using namespace std 标准命名空间,会报编译不通过。
结果: