unary operators overloading there is only one argument
the general form of operator function for unary operators is as follows.
public static return_type operator op (Type t)
{}
Example
public static uop operator -(uop ob)
{
ob.a=-ob.a;
ob.b=-ob.b;
return ob;
}
Example Program
using System;
class uop
{
public int a,b;
public uop(int a1,int b1)
{
a=a1;
b=b1;
}
public void disp(){Console.WriteLine(a);Console.WriteLine(b);}
public static uop operator -(uop ob)
{
ob.a=-ob.a;
ob.b=-ob.b;
return ob;
}
}
class unaryoperator
{
public static void Main(string[] args)
{
uop u=new uop(-10,-10);
u.disp();
uop u1=-u;
u1.disp();
}
}
No comments:
Post a Comment