Write a program in C# to implement the operator overloading for the greater than (>) and assignment (=) operators
using System;
using System.Collections.Generic;
namespace App
{
class Number
{
private double number;
public double DoubleNumber
{
get { return number; }
set { number = value; }
}
public Number() { }
public Number(double number)
{
this.number = number;
}
public static bool operator <(Number otherNumber1, Number otherNumber2)
{
return (otherNumber1.DoubleNumber < otherNumber2.DoubleNumber);
}
public static bool operator >(Number otherNumber1, Number otherNumber2)
{
return (otherNumber1.DoubleNumber > otherNumber2.DoubleNumber);
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter number 1: ");
double number1 = double.Parse(Console.ReadLine());
Console.Write("Enter number 2: ");
double number2 = double.Parse(Console.ReadLine());
Number n1 = new Number(number1);
Number n2 = new Number(number2);
Console.WriteLine("n1 > n2 = {0}", (n1 > n2));
n2 = n1;
Console.WriteLine("n2 = {0}", n2.DoubleNumber);
Console.ReadLine();
}
}
}
Comments
Leave a comment