Console Application using Operator Overload in C# 1.1 and 2.0 (Dot Net)

Posted Apr 09, 2009 by xxris / comments 0 comments / Print / Font Size Decrease font size Increase font size

Console Application using Operator Overload in C# 1.1 and 2.0 (Dot Net)

// Operator overload in C#

using System;

namespace operatoroverload

{

class Class1

{

static void Main()

{

float i,j;

Console.WriteLine("Enter the first value A1");

i=float.Parse(Console.ReadLine());

Console.WriteLine("Enter the first value A2");

j=float.Parse(Console.ReadLine());

op a=new op();

op b=new op();

a.x=i;

b.x=j;

op A3=a+b;

op A4=a-b;

op A5=a*b;

op A6=a/b;

{

if (a>b)

{

Console.WriteLine("A1 is greater than A2");

}

else if(a

{

Console.WriteLine("A2 is greater than A1");

}

}

{

if(a == b)

{

Console.WriteLine(" A1 is equal to A2 ");

}

/* else if(A1 != A2)

{

Console.WriteLine(" A1 is not equal to A2 ");

} */

}

Console.WriteLine(" A1+A2 is {0} ",A3.x);

Console.WriteLine(" A1-A2 is {0} ",A4.x);

Console.WriteLine(" A1xA2 is {0} ",A5.x);

Console.WriteLine(" A1/A2 is {0} ",A6.x);

Console.ReadLine();

}

}

class op

{

public float x;

/*public op(float y)

{

           this.x = y;

        }*/

public static op operator+(op a,op b)

{

op c=new op();

c.x=a.x+b.x;

return(c.x); 

}

public static op operator-(op a, op b)

{

op c = new op();

c.x = a.x-b.x;

return(c.x);

}

public static op operator * (op a,op b)

{

op c=new op();

            c.x = a.x*b.x;

return(c.x);

}

public static op operator/(op a,op b)

{

op c=new op();

c.x=a.x/b.x;

return(c.x);

}

public static bool operator>(op a,op b)

{

            return(a.x > b.x);  

}

public static bool operator < (op a,op b)

{

return(a.x

}

public static bool operator == (op a,op b)

{

return(a.x==b.x);

}

public static bool operator != (op a, op b)

{

return (a.x != b.x);

}

}

}


Rate this Article:

Be the first to rate me.


* You must be logged in order to leave comments, please login or join us.

Comments

No comments yet.



Bookmark and Share
Sign up for our email newsletter
Name:
Email: