I saw that the following two class definitions are claimed to be equivalent. Is that true? How? Thanks.
class complex{
double re, im;
public:
complex( ): re(0) , im(0) { }
complex (double r): re(r), im(0) { }
complex (double r, double i): re(r), im(i) { }
};
class complex{
double re, im;
public:
complex (double r=0, double i=0): re(r), im(i) { }
};
The first one lists all the possibilities (no parameter, one parameter, two parameters) separately (overloading), the second one only needs one function because of usage of default parameters, which is exactly the same but much shorter.
The first one is still useful if initialization of variables is more than just assigning a value to them or you want to have completely different constructors depending on the parameter count.