Operator overloading


  • Every operator has some pre-defined work given by the designers if we assign additional work apart from the existing work to any operator then we say operator is overloaded
Syntax to Overload an operator is:
 
                  type operator operator-symbol (parameter-list)
Example for Operator overloading
  • In general ‘+’ is used to perform addition of 2 numbers but using ‘+’ operator we can’t perform addition of 2 objects of a class. So, we overload our ‘+’ operator to perform addition of 2 objects of a class
  • Consider addition of 2 Complex Numbers like (see table)
  • We cannot create any variables and store Real and Complex parts, So we create a class named ClsComplex with 2 Data Fields int Real, Imaginary
  • Now if we create any object for this class it contains a reference of Real and Imaginary Parts
  • In two objects of the class we store two complex numbers and perform addition of these two objects and store the result in third object

Example  

Class Diagram :-




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAOPverload
{
    class ClsComplex
    {
        int Real, Imaginary;
        public ClsComplex(int R, int I)
        {
            this.Real = R;
            this.Imaginary = I;
        }
        public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
        {
            ClsComplex Num3 = new ClsComplex(Num1.Real + Num2.Real, Num1.Imaginary + Num2.Imaginary);
            return Num3;
        }
        public void Display()
        {
            Console.WriteLine("{0} + {1}i", Real, Imaginary);
        }
    }
    class ClsOverLoad
    {
        static void Main(string[] args)
        {
            ClsComplex C1 = new ClsComplex(1, 4);
            ClsComplex C2 = new ClsComplex(8, 9);
            ClsComplex C3 = C1 + C2;
            Console.Write(" C1=  ");
            C1.Display();
            Console.Write(" C2=  ");
            C2.Display();
            Console.Write(" C3=  ");
            C3.Display();
            Console.Read();
        }
    }
}


output :-


List of Operators that can be overloaded
  • we can redefine the function of most built-in operators globally or on a class-by –class basis. Overloaded operators are implemented as functions
  • The name of an overloaded operator is operatorx,where x is the operator as it appears in the following table. For example, to overload the addition operator, you define a function called operator+. Similarly, to overload the addition/asssignment operator, +=, define a function called operator+=.


Operator
Name
Type
,
Comma
Binary
!
Logical NOT
Unary
!=
Inequality
Binary
%
Modulus
Binary
%=
Modulus assignment
Binary
&
Bitwise AND
Binary
&
Address-of
Unary
&&
Logical AND
Binary
&=
Bitwise AND assignment
Binary
( )
Function call
     --
( )
Cast Operator
Unary
*
Multiplication
Binary
*
Pointer dereference
Unary
*=
Multiplication assignment
Binary
+
Addition
Binary
+
Unary Plus
Unary
++
Increment 1
Unary
+=
Addition assignment
Binary
-
Subtraction
Binary
-
Unary negation
Unary
_
Decrement 1
Unary
_-
Substraction assignment
Binary
->
Member selection
Binary
->*
Pointer-to-member selection
Binary
/
Division
Binary
/=
Division assignment
Binary
< 
Lessthan
Binary
<< 
Left shift
Binary
<<=
Left shift assignment
Binary
<=
Less than or equal to
Binary
=
Assignment
Binary
==
Equality
Binary
> 
Greater than
Binary
>=
Greater than or equal to
Binary
>> 
Right shift
Binary
>>=
Right shift assignment
Binary
[ ]
Array subscript
    --
^
Exclusive OR
Binary
^=
Exclusive OR assignment
Binary
|
Bitwise inclusive OR
Binary
|=
Bitwise inclusive OR assignment
Binary
||
Logical OR

~
One’s complement
Unary
delete
Delete
   --
New
New
   --
Conversion operators
Conversion operators
Unary

List of Operators that can not be overloaded

The operators shown in following table can not be overloaded. The table includes the preprocessor symbols # and ##

Operator
Name
.
Member selection
.*
Pointer-to-member selection
::
Scope resolution
?:
Conditional
#
Preprocessor convert to string
##
Preprocessor concatenate





No comments:

Post a Comment