0

I have a GDI component I use for creating graphical objects on my form.

This component contains a Transformation_Matrix class. When I populate these matrix objects I have the ability to multiply them together to obtain a new transformation matrix.

E.g. Transformation_Matrix matrix1, matrix2, matrix3

... do stuff to populate matrix1 and matrix2

matrix1 * matrix2 = matrix3 // how did he (the programmer) do this?

The programmer who has written the GDI Component has written the Transformation_Matrix class in such a way so that when I consume these matrix objects I can multiply them together and it returns an new Transformation_Matrix object correctly containing the product of mulitipling the two matrices.

How do I build classes in such a way as when they are consumed in some other class or application the consumer/user can multiply my objects and receive a product of the multiplication?

To be more succinct how can I create custom complex objects that allow the ability to be multiplied, divided, added, subtracted, squared (or anything else), much like my GDI component does?

3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by ddanbe
Featured Replies
  • Look up [URL="http://msdn.microsoft.com/en-us/library/aa288467(v=VS.71).aspx"]operators and overloading[/URL]. Read More

  • 2
    ddanbe 2,525   5 Years Ago

    This is how I defined the multiply operator in my complex struct: [CODE=C#] /// <summary> /// Complex multiplication operator facilitates multiplication of two complex numbers. /// Note that overriding this operator automatically overrides the *= operator as well. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static … Read More

2

This is how I defined the multiply operator in my complex struct:

/// <summary>
        /// Complex multiplication operator facilitates multiplication of two complex numbers.
        /// Note that overriding this operator automatically overrides the *= operator as well.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Complex operator *(Complex a, Complex b)
        {
            return new Complex(a._real * b._real - a._imag * b._imag,
                a._real * b._imag + a._imag * b._real);
        }

Be aware that you can also multiply a Complex number with a double, so you have to provide two operatordefinitions for that case as well. But I leave that and the other operators as a nice exercise for you :)

Comments
Good example. Easy to follow
Examples are always helpful
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.