Make a complex number class that demonstrates overloaded operators in C#

This example demonstrates operator overloading, a technique that allows you to define operators such as + and * for objects. Many people use a complex number class as an example because the operators are relatively easy to understand for complex numbers.

This example's Complex class has two public fields Re and Im to hold a complex number's real and imaginary parts.

class Complex
{
public double Re = 0, Im = 0;
...
}

The class has a fairly obvious constructor that initializes the fields.

// Constructor using real and imaginary parts.
public Complex(double real, double imaginary)
{
Re = real;
Im = imaginary;
}

Because a real number is also a complex number (for example, 7 = 7 + 0i), the class also has a constructor that takes a single real number as a parameter.

// Constructor using only a real part.
public Complex(double real)
: this(real, 0)
{
}

The syntax ": this(real, 0)" means C# should invoke some other constructor for the class passing it the parameters real and 0. That lets you use the more general constructor to implement this one without any new code.

So far, we just have code to initialize Complex objects. Now we need to overload operators such as + and * so they can work with Complex objects. The following code shows how the class defines the + operator to add two Complex objects.

// Return A + B.
public static Complex operator +(Complex A, Complex B )
{
return new Complex(A.Re + B.Re, A.Im + B.Im);
}

Operator overloads must be static. The first "Complex" means this operation returns a Complex. That makes sense because a complex number plus another complex number gives you a new complex number.

After the keyword "operator" comes the operator we are defining, in this case +.

The parameters give the variables on which the operator will act. For example, suppose you have the following code.

Complex A = new Complex(1, 2);
Complex B = new Complex(3, 4);
Complex C = A + B;

In that case, when the code executes "A + B" the parameters received by the operator's method are the Complex objects A and B. I often name the first parameter "me" so I can think of it as the operand that is being operated upon, although

The body of the method simply adds the real and imaginary parts of the two parameters and uses the results to return a new Complex object.

Once you understand the syntax, the rest is fairly simple. The following code defines the - and * operators.

// Return A - B.
public static Complex operator -(Complex A, Complex B )
{
return A + (-B );
}

// Return A * B.
public static Complex operator *(Complex A, Complex B )
{
return new Complex(
A.Re * B.Re - A.Im * B.Im,
A.Re * B.Im + A.Im * B.Re);
}

Note that defining the binary - operator (so you can calculate A - B ) does not automatically define a unary - operator (so you can calculate -A). You need to define that operator separately.

// Return -A.
public static Complex operator -(Complex A)
{
return new Complex(-A.Re, -A.Im);
}

The only tricky operator defined by this example is /.

// Return A / B.
public static Complex operator /(Complex A, Complex B )
{
Complex conjugate = new Complex(B.Re, -B.Im);
B *= conjugate;

Complex numerator = A * conjugate;

return new Complex(
numerator.Re / B.Re,
numerator.Im / B.Re);
}

For an explanation of how this works, see for example Multiplying and Dividing Complex Numbers.

This example also defines:

  • A static Parse method that lets you easily parse text values much as int.Parse and double.Parse do.
  • A ToString override that displays a Complex nicely formatted as in "3 + 9i."
  • An overloaded ToString method that lets you pass a format string so you can format the real and imaginary parts as in "1.32 + 13.48i."

These are reasonably straightforward so they aren't described here. See the code for details.

(The next example will continue developing the Complex class so stay tuned...)

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.