类型转换
#include <iostream>
using namespace std;
class Complex
{private:
double real;
double imag;
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex(double r){real=r;imag=0;}
operator double(){return real;}
friend Complex operator+(Complex c1,Complex c2);
void display();
};
Complex operator+(Complex c1,Complex c2)
{ Complex c;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
return c;
}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(23,3.2),c2;
double dl;
dl=c1+1.0;
cout<<dl<<endl;
c2=c1+Complex(3.2);
c2.display();
return 0;
}
/*我想让它既可以用复数的形式输出,又可以做double的形式输出,如何才能实现啊?*/