Operator overloading : Operator overload Binary Plus Subtract : Operator Overload C# Examples


C# Examples » Operator Overload » Operator overload Binary Plus Subtract »

 

Operator overloading









    
using  System;

public  class  Point
{
        public  Point(  int  x,  int  y  )
        {
                this.X  =  x;
                this.Y  =  y;
        }

        public  int  X;

        public  int  Y;

        public  static  Point  operator  +  (  Point  a,  Point  b  )
        {
                return  new  Point(  a.X  +  b.X,  a.Y  +  b.Y  );
        }

        public  static  Point  operator  -  (  Point  a  )
        {
                return  new  Point(  -  a.X  ,  -  a.Y  );
        }

        static  void  Main(string[]  args)
        {
                Point  p  =  new  Point(  3,  4  );
                Point  q  =  new  Point(  36,  -5  );

                Point  r  =  p  +  (  -  q  );

                System.Console.WriteLine(  "Result:  x  =  {0},  y  =  {1}",  r.X,  r.Y  );
        }
}
    
   
  
   



Output

Result: x = -33, y = 9


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Operator Overload
» Operator overload Binary Plus Subtract