C# Interface Sample Program for Operator Overloading

Prabhukiran345
Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 618
using System;
namespace OpOv
{
class Complex
{
public int real;
public int img;
public Complex(int real, int img)
{
this.real = real;
this.img = img;
}
public static Complex operator +(Complex c1,Complex c2)
{
Complex c3 = {FNAMEL}+msdn.microsoft.com">new Complex(c1.real + c2.real, c1.img + c2.img);
return c3;
}
public static Complex operator ~(Complex c)
{Console.WriteLine(c.real+"+i"+c.img);
return c;
//user defined operator can not return void
// user defined operator must be static
}
public static Complex operator >(Complex c1, Complex c2)
{
Complex c3 = {FNAMEL}+msdn.microsoft.com">new Complex(c1.real + c2.real, c1.img + c2.img);
return c3;
}
public static Complex operator <(Complex c1, Complex c2)
{
Complex c3 = {FNAMEL}+msdn.microsoft.com">new Complex(c1.real + c2.real, c1.img + c2.img);
return c3;
}
public static Complex operator *(Complex c1, Complex c2)
{
Complex c3 = {FNAMEL}+msdn.microsoft.com">new Complex(c1.real + c2.real, c1.img + c2.img);
return c3;
}
// only +,-,~ operators are allowed
}
class Demo
{
public static void Main()
{
Complex c1={FNAMEL}+msdn.microsoft.com">new Complex(20,30);
Complex c2={FNAMEL}+msdn.microsoft.com">new Complex(40,50);
Complex c=c1+c2;
c=~c;
//catching the return type of an operator is mandatory
}
}
}

Comments or Responses

Login to post response