默认构造函数
#include<iostream>using namespace std;
class complex
{
public:
double real,imag;
complex(double r,double i)
{
real=r;
imag=i;
}
complex()
{
}
};
complex operator+(complex com1,complex com2)
{
complex temp;
temp.real=com1.real+com2.real;
temp.imag=com1.imag+com2.imag;
return temp;
}
void main()
{
complex com1(1.1,2.2),com2(3.3,4.4),total1,total2;
total1=com1+com2;
total2=operator+(com1,com2);
cout<<"rea1="<<total1.real<<" imag1="<<total1.imag<<endl;
cout<<"rea2="<<total2.real<<" imag2="<<total2.imag<<endl;
}
上面的代码是能够通过的,可我不知道为什么需要写一个默认的无参数的构造函数。