![]() |
#2
xufan2016-11-12 19:42
|
class Compelx
{
friend Complex operator+(double a);
friend Complex operator-(double a);
friend Complex operator-(const Complex &c);
double r;
double i;
public:
Complex(double a,double b) : r(a),i(b){}
Complex operator+(const Complex &c) const
{
return Complex(r+c.r,i+c.i);
}
Complex operator-(const Complex &c) const
{
return Complex(r-c.r,i-c.i);
}
Complex operator+(double a) const
{
return Complex(r+a,i);
}
Complex operator-(double a) const
{
return Complex(r-a,i);
}
};
Complex operator+(double a)
{
return Complex(a+r,i);
}
Complex operator-(double a)
{
return Complex(a-r,-i);
}
Complex operator-(const Complex &c)
{
return Complex(-c.r,-c.i);
}
int main()
{
Complex c1(5.6,4.5);
Complex c2(2.3,3.3);
Complex c1 + c2;
Complex c1 - c2;
double a = 2.3;
Complex c1 + a;
Complex c1 - a;
operator+(a,c1);
operator-(a,c2);
operator-();
}