关于转换构造函数的问题!
#include<iostream>using namespace std;
class Complex
{
public:
Complex();
Complex(int,int);
~Complex();
operator double(){return real;} //类型转换函数
friend Complex operator+(Complex&,Complex&);
Complex(double r){real=r;imag=0;}
void display();
private:
int real;
int imag;
};
Complex operator+(Complex&a,Complex&b)
{
return Complex(a.real+b.real,a.imag+b.imag);
}
void Complex::display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
Complex::Complex()
{
real=0;
imag=0;
}
Complex::Complex(int a,int b):real(a),imag(b){}
Complex::~Complex(){}
int main()
{
Complex a(1,2),b(2,3);
Complex c;
a.display();
c=a+Complex(3);
c.display();
getchar();
}