This program helps you to implement operator overloading with less than (<) and greater than (>) operators. We have created two objects and assign values 9 and 7 to those operators. After comparing these objects displaying the true and false value.
In case of any problem while implementation, you can contact us by commenting to this post.
Oprator Overloading with Less than (<) and Greater than Operators in C# Example:
using System;
using System.Collections.Generic;
using System.Text;
namespace Console_App
{
public class Class1
{
public static void Main()
{
Class2 a1 = new Class2(9);
Class2 a2 = new Class2(7);
Console.WriteLine("Example 1:");
Console.WriteLine();
if (a1 > a2)
Console.WriteLine("Result: true");
else
Console.WriteLine("Result: false");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Example 2:");
Console.WriteLine();
if ( a2 > a1)
Console.WriteLine("Result: true");
else
Console.WriteLine("Result: false");
Console.ReadLine();
}
}
public class Class2
{
public int i;
public Class2(int j)
{
i = j;
}
public static bool operator >(Class2 x1, Class2 x2)
{
System.Console.WriteLine(" " + x1.i + " > " + x2.i);
return x1.i > x2.i;
}
public static bool operator <(Class2 x1, Class2 x2)
{
System.Console.WriteLine(" " + x1.i + " < " + x2.i);
return x1.i < x2.i;
}
}
}
No comments:
Post a Comment