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?

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 :)

commented: Examples are always helpful +1
commented: Good example. Easy to follow +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.