Wednesday, January 1, 2014

Operator Overloading

Operator overloading is done to perform special operations relative to classes in the program.When an operator is overloaded, none of its original meanings are lost. Instead, the type of objects it can be applied to is expanded.For example, a class that maintains a stack mightoverload + to perform a push operation and – – to perform a pop.

The ability to overload operators is one of C++'s most powerful features. It allows
the full integration of new class types into the programming environment. After
overloading the appropriate operators, objects in expressions are used in just the
same way as C++'s built-in data types are used. Operator overloading also forms the
basis of C++'s approach to I/O.
 Operators can be overloaded by creating operator functions. An operator function defines
the operations that the overloaded operator will perform relative to the
class upon which it will work. An operator function is created using the keyword
operator. Operator functions can be either members or nonmembers of a class.
Nonmember operator functions are almost always friend functions of the class,
however.