Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
Program illustrates operator overloading in C#. Operator overloading allows the programmer to use the built-in operators for user-defined types.
Program: Complex class operators.
To overload the built-in + and * operators so that they may be used with Complex operands, we define static methods called operator+ and operator*. Given Complex variables c, d and e the expression c+d*e calls the method operator* to compute the product of d and e, and then calls the method operator+ to compute the final sum.