#include
using namespace std;
class Complex
{
public:
Complex operator +(Complex& c2);
//运算符的重载也可以进行函数重载
Complex operator +(int a);
void get_complex();
void dispaly();
private:
int real;
int imag;
};
Complex Complex::operator+(Complex& c2)
{
Complex c1;
c1.real = real + c2.real;
c1.imag = imag + c2.imag;
return c1;
}
Complex Complex::operator+(int a)
{
Complex c1;
c1.real = real + a;
c1.imag = imag;
return c1;
}
void Complex::get_complex()
{
cin >> real >> imag;
}
void Complex::dispaly()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1, c2, c3;
int a = 0;
c1.get_complex();
c2.get_complex();
c3 = c1 + c2;
c3.dispaly();
c2.get_complex();
cin >> a;
c3 = c2 + a;
c3.dispaly();
}
2简答题)有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如: c=a+b。
#include
using namespace std;
class Juzheng
{
public:
friend Juzheng operator+(Juzheng a,Juzheng b);
void set();
void display();
private:
int arr[2][3];
};
Juzheng operator+(Juzheng a,Juzheng b)
{
Juzheng c;
for(int i=0;i<2;i++)
{
for (int j = 0; j < 3; j++)
{
c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
}
}
return c;
}
void Juzheng::set()
{
for (int i = 0; i<2; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> arr[i][j];
}
}
}
void Juzheng::display()
{
for (int i = 0; i<2; i++)
{
for (int j = 0; j <=3; j++)
{
cout< }
}
}
int main()
{
Juzheng c,a,b;
a.set();
a.display();
b.set();
b.display();
c = a + b;
c.display();
return 0;
}



