关于构造函数的一个问题
#include<iostream>using namespace std;
class complex{
public:
complex(){real=0;imag=0;}
complex(double i,double j){real=i;imag=j;}
complex operator + (complex &t1);
complex operator - (complex &t1);
complex operator * (complex &t1);
complex operator / (complex &t1);
void display();
private:
double real;
double imag;
};
complex complex:: operator + (complex &t1)
{complex t2;
t2.real=real+t1.real;
t2.imag=imag+t2.imag;
return t2;
}
complex complex:: operator - (complex &t1)
{complex t2;
t2.real=real-t1.real;
t2.imag=imag-t2.imag;
return t2;
}
complex complex:: operator * (complex &t1)
{complex t2;
t2.real=real*t1.real;
t2.imag=imag*t2.imag;
return t2;
}
complex complex:: operator / (complex &t1)
{complex t2;
t2.real=real/t1.real;
t2.imag=imag/t2.imag;
return t2;
}
void complex::display()
{cout<<real<<"+"<<imag<<'i'<<endl;}
int main()
{complex t1(1,2),t2(3,4),t3;
t3=t1+t2;
cout<<"t1与t2之和是:";
t3.display();
t3=t1-t2;
cout<<"t1与t2之差是:";
t3.display();
t3=t1*t2;
cout<<"t1与t2之积是:";
t3.display();
t3=t1/t2;
cout<<"t1与t2之商是:";
t3.display();
return 0;
}
为什么这个题目的构造函数一定要有第一个呢?
我是一个c++的初学者,请大家帮帮忙啊!