定义一个复数类complex。
发布网友
发布时间:2022-05-01 20:50
我来回答
共2个回答
热心网友
时间:2022-06-23 05:03
模仿运算/
//类中声明
Complex operator +(Complex &); //重载运算符+
Complex operator -(Complex &); //重载运算符-
//实现
Complex Complex::operator +( Complex &c)
{
Complex t;
t.Real = this->Real + c.Real;
t.Image = this->Image + c.Image;
return t;
}
Complex Complex::operator -( Complex &c)
{
Complex t;
t.Real=this->Real - c.Real;
t.Image = this->Image - c.Image;
return t;
}
热心网友
时间:2022-06-23 05:04
Complex operator +(const Complex & t1,const Complex & t2)
{
Complex term(0,0);
term.real=t1.real+t2.real;
term.image=t1.image+t2.image;
return term;
}
Complex operator -(const Complex & t1,const Complex & t2)
{
Complex term(0,0);
term.real=t1.real-t2.real;
term.image=t1.image-t2.image;
return term;
}
Complex operator -(const Complex & t)
{
Complex term(0,0);
term.real=-t.real;
term.image=-t.image;
return term;
}
void Complex::show()
{
cout<<real<<"+"<<image<<'i'<<endl;
}