Operator Overloading

Operator Overloading
·         Operator overloading means we are extending the value of an operator.
·         So that it can be used for user defined functions.
·         Operator overloading is the ability to tell the compiler how to perform a certain operation when its corresponding operator is used on one or more variables.
·         It means the behavior of operators when applied to objects of a class can be redefined. It is similar to overloading functions except the function name is replaced by the keyword operator followed by the operator’s symbol. There are 5 operators that are forbidden to overload. They are :: . .* sizeof ?:

Syntax:

<return> <type operator> op();
Example:
int operator –();
·         The keyword operator tell the compiler that “-” operator is overloaded.
·        
    Operator overloading means, 
    meaning of an operator is not
    changed but value of this 
    operator is extended.
 
To invoke this operator we use following operator.
    -o1;
For Example:
#include<iostream.h>
#include<conio.h>
Class overload
{
int ab,ac;
public:
overload()
{
ab=-123;
ac=222;
}
void operator –()      //operator is overload here
{
Ab=-ab;
ac=-ac;
}
void show()
{
cout<<ab;
cout<<ac;
}
};
void main()
Output:
Ab=123
Ac=-222

 
{
Overload ol;
-ol;
ol.show();
getch();

}

Post a Comment

Previous Post Next Post