In Function overloading, n number of functions can be created for same class. But the signatures of each function should vary. For example
public class Employee
{
public void Employee()
{ }
public void Employee(String Name)
{ }
}
We had seen function overloading in the previous example. For operator Overloading , we will have look at the example below. We define a class rectangle with two operator overloading methods.
class Rectangle
{
private int Height;
private int Width;
public Rectangle(int w,int h)
{
Width=w;
Height=h;
} }
Public static bool operator >(Rectangle a, Rectangle b)
{
return a.Height > b.Height ;
}
public static bool operator <(Rectangle a,Rectangle b)
{
return a.Height < b.Height ;
} }
Let us call the operator overloaded functions from the method below. When first if condition is triggered, first overloaded function in the rectangle class will be triggered. When second if condition is triggered, second overloaded function in the rectangle class will be triggered.
public static void Main()
{
Rectangle obj1 =new Rectangle();
Rectangle obj2 =new Rectangle();
if(obj1 > obj2){
Console.WriteLine("Rectangle1 is greater than Rectangle2");
}
if(obj1 < obj2)
{
Console.WriteLine("Rectangle1 is less than Rectangle2");
}}