Operator Overloading in C#
Here is simple and easy example of operator overloading.
using System;
class Rectangle
{
private int iHeight;
private int iWidth;
{
private int iHeight;
private int iWidth;
public Rectangle()
{
Height=0;
Width=0;
}
public Rectangle(int w,int h)
{
Width=w;
Height=h;
}
{
Height=0;
Width=0;
}
public Rectangle(int w,int h)
{
Width=w;
Height=h;
}
public int Width
{
get
{
return iWidth;
}
set
{
iWidth=value;
}
}
public int Height
{
get
{
return iHeight;
}
set
{
iHeight=value;
}
}
{
get
{
return iWidth;
}
set
{
iWidth=value;
}
}
public int Height
{
get
{
return iHeight;
}
set
{
iHeight=value;
}
}
public int Area
{
get
{
return Height*Width;
}
}
{
get
{
return Height*Width;
}
}
/* OverLoading == */
public static bool operator==(Rectangle a,Rectangle b)
{
return ((a.Height==b.Height)&&(a.Width==b.Width));
}
{
return ((a.Height==b.Height)&&(a.Width==b.Width));
}
/* OverLoading != */
public static bool operator!=(Rectangle a,Rectangle b)
{
return !(a==b);
}
{
return !(a==b);
}
/* Overloding > */
public static bool operator>(Rectangle a,Rectangle b)
{
return a.Area>b.Area;
}
{
return a.Area>b.Area;
}
/* Overloading < */
public static bool operator < (Rectangle a,Rectangle b)
{
return !(a > b);
}
public static bool operator < (Rectangle a,Rectangle b)
{
return !(a > b);
}
/* Overloading >= */
public static bool operator >= (Rectangle a,Rectangle b)
{
return (a>b)||(a==b);
}
{
return (a>b)||(a==b);
}
/* Overloading <= */
public static bool operator <= (Rectangle a,Rectangle b)
{
return (a < b)||(a==b);
}
{
return (a < b)||(a==b);
}
public override String ToString()
{
return "Height=" + Height + ",Width=" + Width;
}
public static void Main()
{
Rectangle objRect1 =new Rectangle();
Rectangle objRect2 =new Rectangle();
Rectangle objRect3 =new Rectangle(10,15);
objRect1.Height=15;
objRect1.Width=10;
objRect2.Height=25;
objRect2.Width=10;
Console.WriteLine("Rectangle#1 " + objRect1);
Console.WriteLine("Rectangle#2 " + objRect2);
Console.WriteLine("Rectangle#3 " + objRect3);
{
return "Height=" + Height + ",Width=" + Width;
}
public static void Main()
{
Rectangle objRect1 =new Rectangle();
Rectangle objRect2 =new Rectangle();
Rectangle objRect3 =new Rectangle(10,15);
objRect1.Height=15;
objRect1.Width=10;
objRect2.Height=25;
objRect2.Width=10;
Console.WriteLine("Rectangle#1 " + objRect1);
Console.WriteLine("Rectangle#2 " + objRect2);
Console.WriteLine("Rectangle#3 " + objRect3);
if(objRect1==objRect2)
{
Console.WriteLine("Rectangle1 & Rectangle2 are Equal.");
}
else
{
if(objRect1 > objRect2)
{
Console.WriteLine("Rectangle1 is greater than Rectangle2");
}
else
{
Console.WriteLine("Rectangle1 is lesser than Rectangle2");
}
}
{
Console.WriteLine("Rectangle1 & Rectangle2 are Equal.");
}
else
{
if(objRect1 > objRect2)
{
Console.WriteLine("Rectangle1 is greater than Rectangle2");
}
else
{
Console.WriteLine("Rectangle1 is lesser than Rectangle2");
}
}
if(objRect1==objRect3)
{
Console.WriteLine("Rectangle1 & Rectangle3 are Equal.");
}
else
{
Console.WriteLine("Rectangle1 & Rectangle3 are not Equal.");
}
}
}
{
Console.WriteLine("Rectangle1 & Rectangle3 are Equal.");
}
else
{
Console.WriteLine("Rectangle1 & Rectangle3 are not Equal.");
}
}
}
Output:
=======
Rectangle#1 Height=15,Width=10
Rectangle#2 Height=25,Width=10
Rectangle#3 Height=15,Width=10
Rectangle1 is lesser than Rectangle2
Rectangle1 & Rectangle3 are Equal.
=======
Rectangle#1 Height=15,Width=10
Rectangle#2 Height=25,Width=10
Rectangle#3 Height=15,Width=10
Rectangle1 is lesser than Rectangle2
Rectangle1 & Rectangle3 are Equal.
0 comments:
Post a Comment