类类型转换
#include <iostream>
using namespace std;
class Complex
{public:
Complex(double r=0,double i=0):real(r),imag(i){}
Complex(int r){real=r;imag=0;}
friend Complex operator+(Complex a,Complex b);/*friend Complex operator+(Complex &a,Complex &b);这样写为什么会出错啊*/
void display();
private:
double real;
double imag;
};
Complex operator+(Complex a,Complex b)
{Complex c;
c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return c;}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(1.0,2.4),c2(2.3,4),c3;
c3=6+c1; /*如果我这里改成c3=c1+c2就不会错为什么呢*/
cout<<"c1+c2=";c3.display();
return 0;
}