What is operator?
Operators are the symbols, which are used to perform an operation between the operands. As,
a + b
Operands : a and b
Operator : +
Type of operators:
Operators are of two types, which are:
Unary Operators : ++, --, -
Binary Operators : +, -, *, /, >, <, =, == etc.
Why operator overloading is required?
Usually, the operators work with the pre-defind type like int, float, string etc. If you want to perform an operation on the user defined type (object), it is required to re-define the operators (overload) to perform the operation.
- Number n1=new Number(10,20);
- Number n2=new Number(5,10);
- Number n3=n1+n2;
n1 and n2 are two user defined types (object) and + operator is required to perform an addition between the two objects. This is only possible with the operator overloading.
Example:
Create a class to define a user defined type (object).
- class Numbers
- {
- public int num1, num2;
- public Numbers()
- {
- num1 = 0;
- num2 = 0;
- }
- public Numbers(int a,int b)
- {
- num1 = a;
- num2 = b;
- }
- public void PrintNumbers()
- {
- Console.WriteLine("Num1 :"+num1);
- Console.WriteLine("Num2 :" + num2);
- }
- }
Write the code to redefine the operators (overload) to perform a user defined operation.
Operator overloading : +
- public static Numbers operator +(Numbers ob1, Numbers ob2)
- {
- Numbers ob3 = new Numbers();
- ob3.num1 = ob1.num1 + ob2.num1;
- ob3.num2 = ob1.num2 + ob2.num2;
- return ob3;
- }
Operator overloading : ++
- public static Numbers operator ++(Numbers ob1)
- {
- Numbers ob2 = new Numbers ();
- ob2.num1 = ob1.num1 + 1;
- ob2.num2 = ob1.num2 + 1;
- return ob2;
- }
Operator overloading : >
- public static bool operator >(Numbers ob1, Numbers ob2)
- {
- if (ob1.num1 > ob2.num1 && ob1.num2 > ob2.num2)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
Operator overloading : <
- public static bool operator <(Numbers ob1, Numbers ob2)
- {
- if (ob1.num1 < ob2.num1 && ob1.num2 < ob2.num2)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
Write the statement in Main method to test the Application.
Statement 1 - To test + operator overloading.
- Numbers n1 = new Numbers();
- Numbers n2 = new Numbers(10,20);
- Numbers n3 = new Numbers(20,30);
- n1 = n2 + n3;
- n1.PrintNumbers();
Statement 2 - To test ++ operator overloading.
- Numbers n1 = new Numbers();
- Numbers n2 = new Numbers(10,20);
-
- n1 = ++n2;
- n1.PrintNumbers();
- n2.PrintNumbers();
-
-
- n1 = n2++;
- n1.PrintNumbers();
- n2.PrintNumbers();
Statement 3 - To test >operator overloading.
- Numbers n1 = new Numbers(10,20);
- Numbers n2 = new Numbers(20,30);
- if (n1 > n2)
- {
- Console.WriteLine("True");
- }
- else
- {
- Console.WriteLine("False");
- }
Statement 4 - To test < operator overloading.
- Numbers n1 = new Numbers(10,20);
- Numbers n2 = new Numbers(20,30);
- if (n1 < n2)
- {
- Console.WriteLine("True");
- }
- else
- {
- Console.WriteLine("False");
- }
Note
Some of the operators are not overloaded. As- =, ?:,->, sizeof, new etc.The comparison operators must be overloaded in the pairs.
That's all. Thank you. If any suggestion or clarification is required, kindly revert back with the comments.