Operator Overloading With Example Syntax And Program

Explain Operator Overloading With Example Syntax and Program

Operator Overloading :-
1     It’s a type of Polymorphism, where we can give additional meaning or implementation to existing operators.
2.     But the usage of Overloaded Operators should follow the usual rules laid down by the compiler.
3.    Operator overloading is accomplished by means of special kind of function. Operator overloading can be carried out by means of either member function or friend function.

     Syntax: return_type  operator  operator t o be overloaded( parameters);
  Eg:  void operator ++( )    // same as void increment();

/*4.     When using Type-Casting operators the following points need to be remembered :-
a.     It can Not be declared as Friend, so it has to be a member function.
b.     No return-type not even void.
c.     No input-arguments not even void.  */
Rules for overloading operators:-
•    Only those operators that are predefined in the c++ compiler can be overloaded. New operators cannot be created such as ( $ ,# , @).
•    Overloaded operator can not take default arguments.
•    We can’t change the basic meaning of an operator, i.e., we can’t redefine the + to subtract one value from the other value.
•    We cannot overload any preprocessor symbol such as  #.
•    We can not change the precedence or grouping of an operator nor we can change the no of arguments it expects.
•    Unary operators overloaded by means of a member function take no arguments and return no values. But unary operators overloaded by means of a friend functions take one reference argument.
•    Binary operators overloaded through a member functions take one argument and those, which are over loaded through a friend function, take 2 arguments.
•    Overloaded operators follow the syntax rules of the original operators. They can’t be overridden
•    Binary arithmetic operators such as +, -, *, / must explicitly return a value.
•    There are some operators that can’t be overloaded (::, *, . , ?:, sizeof().
•    Using friend functions to overload >> and << operators
The following is an example program of overloading relational operator  == to compare the 2 given complex No’s.
    OPERATOR            MEANING
        Sizeof ( )            size of operator
        .                Membership operator
        .*                Pointer to member operator
        ::                scope resolution operator
        ?:                Ternary (or)conditional operator

it is possible to overload operators by using special member function “operator “ or a friend function calling operator member function.
    The general form of overloaded operator member function is as follows
                 return type class name : : operator  op(argument list)
The assignment operator has some additional restriction. It can be overloaded as a non-static member function, not as friend function. It is the only operator that can not be inherited. A derive class can not use a base class assignment operator.

Operators that cannot be overloaded as friend.
1.    =   operator(assignment operator)
2.    ( )  operator (function call operator)
3.    [ ]  operator  (subscript operator)
4.     operator (arrow operator)

Ex:   class sample
        {  int  x;
            sample()
    { x=10; }
void operator +(int I )
 void add( int I)
{  x=x+I;
}
void show( )
{  cout<<x;
}
void main()
{
  sample obj;
  obj.add(20)     //obj+10;
obj.show();
getch();
}
output: 30.


Labels: