Recently I have done an assignment, on overloading basic functionalists(+,-,conjugate…) of complex class using templates. I had to sweat out a little to find out the proper return type, (casting to higher type), but at the end, I got it working perfectly.
This is how my class looks like –
template <typename T> class complex_t
{
private:
T real;
T imaginary;
public:
complex_t(T X, T Y)
{
real=X;
imaginary=Y;
}
}
But I didnt get full marks, because I didn’t implement the +=, -= etc. operators. Why is it important to implement those operators? Whether doing that really provide any particular benefits?
Can anyone share some thoughts?
Thanks in advance,
+=and friends work in-place, so you don’t have to return a new instance of your class. For complex numbers, that might not be much of a problem, but with larger structures the copying might be expensive.As an example, suppose that you’re implementing vectors, in the mathematical sense, supporting arbitrary lengths.