Overloading is one of the best concept introduced in OOPs and it also supports Operator overloading. Sometime we don’t want actual functionality of operator in that case we need to overload that operator. For example in case of ‘+’ operator I want to multiple the result with 2 that is:

double opp1=5, opp2=10, result=0;

Actual result:

result = opp1 + opp2;  // 15

I want:

result = opp1 + opp2; // 30

{
static void Main()
{
Operators opp1, opp2, result;
Console.WriteLine(“Please enter two values: “);

// user input
opp1 = new Operators(Convert.ToDouble(Console.ReadLine()));
opp2 = new Operators(Convert.ToDouble(Console.ReadLine()));

// + operator overloading, (first sum two values and then double this value)
result = opp1 + opp2;
result.Show(“Result for operator ‘+’ :”);

// + operator overloading, (first sum two values and then double this value)
result = opp1 – opp2;
result.Show(“Result for operator ‘-‘ :”);

// + operator overloading, (first sum two values and then double this value)
result = opp1 * opp2;
result.Show(“Result for operator ‘*’ :”);

// + operator overloading, (first sum two values and then double this value)
result = opp1 / opp2;
result.Show(“Result for operator ‘/’ :”);
}
}

class Operators
{
// variable declaration
double opp1, result;

// default constructor
public Operators()
{
}

// parameterized constructor
public Operators(double x)
{
opp1 = x;
}

// method that will display final result
public void Show(string msg)
{
Console.WriteLine(msg + result);
}

// method that overloads + operator’s actual functionality
public static Operators operator +(Operators x, Operators y)
{
Operators op = new Operators();
op.result = (x.opp1 + y.opp1) * 2;
return op;
}

// method that overloads – operator’s actual functionality
public static Operators operator -(Operators x, Operators y)
{
Operators op = new Operators();
op.result = (x.opp1 – x.opp1) * 2;
return op;
}

// method that overloads * operator’s functionality
public static Operators operator *(Operators x, Operators y)
{
Operators op = new Operators();
op.result = (x.opp1 * y.opp1) * 2;
return op;
}

// method that overloads / operator’s functionality
public static Operators operator /(Operators x, Operators y)
{
Operators op = new Operators();
op.result = (x.opp1 / y.opp1) * 2;
return op;
}
}