class Complex//复数类,重载运算符使其能进行复数运算
{
private:
double x;
double y;
public:
double GetValuex(){return x;};
double GetValuey(){return y;};
void SetValue(double m0,double n0){x=m0;y=n0;};
public:
Complex operator +(Complex m);
Complex operator -(Complex m);
Complex operator *(Complex m);
Complex operator /(Complex m);
};
Complex Complex::operator +(Complex m)//重载 +
{
Complex n;
n.x=x + m.x;
n.y=y + m.y;
return n;
}
Complex Complex::operator -(Complex m)//重载 -
{
Complex n;
n.x=x - m.x;
n.y=y - m.y;
return n;
}
Complex Complex::operator *(Complex m)//重载 *
{
Complex n;
n.x=x*m.x+y*m.y;
n.y=x*m.y+y*m.x;
return n;
}
Complex Complex::operator /(Complex m)//重载 /
{
Complex n;
n.x=(x*m.x-y*m.y)/(m.x*m.x+m.y*m.y);
n.y=(y*m.x-x*m.y)/(m.x*m.x+m.y*m.y);
return n;
}
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
Complex a,b,c;
a.SetValue(1,1);
b.SetValue(2,2);
c=a*b;
cout<<c.GetValuex();
cout<<c.GetValuey();
return 0;
}