求助:c++一点问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 03:12:18
怎样重载下面两个:
1.通过重载>>和<<运算符来分别输入和输出复数
2.重载= =和!=,以允许比较两个复数

重载函数怎么写

// operator >>
istream& operator >> (istream& i,complex& rhs)
{
cout<<"输入和实和虚:";
i>>rhs.real;
i>>rhs.imag;
return i;
}
//operator >>
ostream& operator << (ostream& o, complex& rhs)
{
o<<"实:";
o<<rhs.real;
o<<" 虚:";
o<<rhs.imag;
return o;
}

//operator ==
bool complex::operator==(const complex& rhs) const
{
return (real == rhs.real && imag == rhs.imag) ;
}

//operator !=
bool complex::operator!=(const complex& rhs) const
{
return (real != rhs.real || imag != rhs.imag) ;
}