Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Operator Overloading

Program gif illustrates operator overloading in C#. Operator overloading   allows the programmer to use the built-in operators for user-defined types.

   program56845
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.


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.