这是代码:正如您在代码中看到的,构造函数被用作返回类型,但我已经读过它什么也不返回,甚至不是 void。请解释。
#include<iostream>
using namespace std;
class complex_1
{
float x,y;
public:
complex_1() {}
complex_1(float a) {x=y=a;}
complex_1(float real, float imag )
{x=real;y=imag;}
complex_1 operator+(complex_1);
void display(void);
};
complex_1 complex_1::operator+(complex_1 c)
{
c.x= x + c.x;
c.y= y + c.y;
return c;
}
void complex_1::display(void)
{
cout<<x<<" + "<<y<<"j"<<endl;
}
int main()
{
complex_1 A(2.7, 3.5);
complex_1 B(1.2);
complex_1 C;
//C=A.operator+(B); // equivalent to C = A + B;
C=A+B;
cout<<"A = ";
A.display();
cout<<"B = ";
B.display();
cout<<"C = ";
C.display();
return 0;
}