Sponsored Ad

Friday, October 23, 2009

Operator Overloading

Operator overloading is something that will be familiar to developers of C + +. However, because the concept is new to Java and Visual Basic developers, find out here. C + + probably prefer to move to the main example of operator overloading.

The point of operator overloading is not always just want to call methods or properties objects. Often, you need to do things like adding numbers together, multiply them, or making logical operations such as comparing objects. Suppose you have defined a class that represents a mathematical matrix. Now, in the world of mathematics, the arrays can be added and multiplied, like numbers. Therefore, it is quite likely that you want to write code like this:

Matrix a, b, c;
// assume a, b and c have been initialized
Matrix d = c * (a + b);

By overloading operators, you can tell the compiler + and * what to do when used in conjunction with a Matrix object, which lets you write code as above. If you were coding in a language not does not support operator overloading, it would have to define the methods for carrying out such operations. The figure would probably be less intuitive and would probably be something like this:

Matrix d = c.Multiply(a.Add(b));

With what we have learned so far, operators like + and * are strictly for use with the default data types, and for good reason: The compiler knows what all the common operators mean for data types. For example, knows how to add two long so how to divide a double by another double, and can generate the intermediate language code. In defining its own classes or structures, however, you have to tell the compiler of all, what methods are available for calls, What fields to store each instance, and so on. Similarly, if you want to use its own operators , they will only tell the compiler what the relevant operators means in the context of that class. The way to do it by defining operator overloads.

The other thing they should stress is that overloading isn't concerned with arithmetic operators. You also require to consider the comparison operators, == , < , > , != , > = , and < = . Take the statement if (a==b) .
For classes, this statement will, by default, compare the references a and b . It tests to see if the references point to the same location in memory, than checking to see if the instances actually contain the same data. For the string class, this behavior is overridden so that comparing strings does compare the contents of each string. You might require to do the same for your own classes. For structs, the == operator does not do anything at all by default. Trying to compare one structs to see if they are equal produces a compilation error unless you explicitly overload == to tell the compiler how to perform the comparison.

A gigantic number of situations exist in which being able to overload operators will permit you to generate more readable and intuitive code, including:

  • Almost any mathematical object, such as coordinates, vectors, matrices, tensors, functions, and etc. If you are writing a program that does some physical or mathematical models, he certainly use the classes that represent these objects.
  • Graphics programs that use mathematical coordinates - or objects associated with the calculation of positions on the screen.
  • A class that represents an amount of money (for example, in a financial program).
  • A word processor or text analysis program that uses the classes that represent the sentences, clauses, and so on, you may want to use the operators to combine words (a more sophisticated version  string concatenation).

However, there are many types for which that operator overloading would be irrelevant. Use inappropriate operator overloading will the code using their rates much more difficult to  understand. For example, multiplying two DateTime objects simply does not make sense conceptually.

1 comments:

  1. Very nice tutorial and also site, I am not sure if this will help, but here is a link with some code in it that may help the operator

    http://www.codingfriends.com/index.php/2009/07/24/operator/

    It usually helps me when I have code examples with theory :).

    Once again very nice site.

    ReplyDelete

Sponsored Ad

Website Update

Followers