I was looking for a way to calculate the addition of complex numbers and I saw this example but there is a part which I don't understand. this is the whole code for this class:
class Complex
{
public int real, imaginary;
public Complex()
{
}
public Complex(int tempReal , int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
public Complex addComp(Complex C1 , Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
}
The part where it is written :
public Complex addComp(Complex C1 , Complex C2)
My question is first is this a constructor or method? and my next question is why is Complex written behind addComp? how is that possible and what is the point of it?
addComp
is a method, but a very badly written one!
Observations:
addComp
should either be static
and take two parameters, or be non-static
and take one; an instance method shouldn't ignore the this
value, most timesaddComp
should almost certainly be the binary +
operator, which would be static
in the above scenario (C# allows you to overload many operators, including +
)Complex
should probably be a readonly struct
, not a class
- with suitable operators and overloads, support for IEquatable<Complex>
, etcreal
and imaginary
should be properties, not public fields