6.7 Overloading of Operators and Functions

Operator overloading, so popular in C++, is a way to implement polymorphism for some of the unary and binary operators in a language. The overloading operates at two levels. For example, consider the four arithmetic operators +, −, * and /. Even in C language these operators are overloaded in the sense that though for both integers and floats the meaning of ‘+’ operator is “addition of numerical value”, the actual details of the operation, exceptional condition, range of valid results, etc. are quite different for them.

Though not available, we can have further overloading of ‘+’ in some hypothetical language, for concatenation of strings, i.e., “hello” + “world” will produce “hello world”. This is almost intuitive and quite useful. Further, we can overload the ‘*’ operator for strings, such that “world”*3 will give “world-world-world”. This overloading seems reasonable because a string is isomorphic to an integer – if we concatenate two strings their lengths get added.

If we consider a slightly more esoteric domain, the ‘+’ operator can be over loaded for matrices also, as it is done in matrix manipulation languages like MATLAB, R, PDL, etc. We can even contrive to assign a reasonable meaning to the ‘−’ operator.

Such operator overloading becomes our concern here, as it can complicate the type checking. Suppose we write A + 5 where A is m × n matrix. How do we interpret it? In specialized programming languages like MATLAB, the interpretation could be “add value 5 to each element of A”. However, how does the compiler know that the programmer has not made a mistake? Should it flag this construct as an error? or as a warning?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93