重载的<<运算符

来源:互联网 发布:阿里云服务器租用费用 编辑:程序博客网 时间:2024/05/17 03:47

//这个还有不懂得地方,以后要继续研究

#include<iostream.h>
//using namespace std;//???
class Complex
{
public:
 Complex(){real=0;imag=0;}
 Complex(double r,double i){real=r;imag=i;}
 Complex operator+(Complex &c2);
 friend ostream& operator<<(ostream&,Complex&);
private:
 double real;
 double imag;
};
Complex Complex::operator+(Complex&c2)
{
 return Complex(real+c2.real,imag+c2.imag);
}
ostream & operator<<(ostream& output,Complex& c)//???
{
 output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
 return output;//???
}
int main()
{
 Complex c1(2,4),c2(6,10),c3;
 c3=c1+c2;
 cout<<c3;
}

原创粉丝点击