Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator Overloading.

Similar presentations


Presentation on theme: "Operator Overloading."— Presentation transcript:

1 Operator Overloading

2 Introduction C# supports operator overloading, it means user defined data type like class and structure can be use much as the same way as built-in type with operators Eg: Class A { ….. } A p,q,r; r=p+q;

3 Overloadable Operators
CATEGORY OPERATORS Binary Arithmetic +,-,*,/,% Unary Arithmetic +,-,++,-- Binary Bitwise &,|,^,<<,>> Unary Bitwise !,~,true,false Logical Operators ==,!=,>=,<=,<,>

4 Operators that can not be overloaded
CATEGORY OPERATORS Conditional Operators &&,|| Compound Assignment +=,-=,*=,/=,%= Other Operators [],(),=,?:,->,new, sizeof,typeof,is, as

5 Defining Operator Overloading
To define additional task to an operator, we must specify what it means in relation to the class This is done with the help of special method called operator method The general form of this method is Public static retval operator op (arglist) { method body; //task defined }

6 Cont.. They must be defined as public static
The retval (return value) type is the type we get when we use this operator, but technically it can be of any type The arglist is the list of arguments passed One argument for unary operator Two arguments for binary operator In the case of unary operators, the argument must be the same type of the enclosing class or structure In the case of binary operators, first argument must be the same as class, and second may be of any type Eg: Public static vector operator + (vector a, vector b) //unary minus Public static vector operator – (vector a) // Comparison Public static bool operator == (vector a, vector b)

7 Overloading unary operator

8 public Space(int a, int b, int c) x = a; y = b; z = c; }
class Space { int x, y, z; public Space(){} public Space(int a, int b, int c) x = a; y = b; z = c; } public void display() Console.WriteLine("X=" + x + "y=" + y + "z=" + z); public static Space operator - (Space s) Space temp=new Space(); temp.x = -s.x; temp.y = -s.y; temp.z = -s.z; return (temp); class Program { static void Main(string[] args) Space s = new Space(10, -20, 30); Console.WriteLine("B4 overloading "); s.display(); s = -s; Console.WriteLine("After overloading"); Console.Read(); }

9 Overloading Binary Operator

10 class complex { double x, y; public complex() { } public complex(double real, double imag) x = real; y = imag; } public static complex operator +(complex c1, complex c2) complex c3 = new complex(); c3.x = c1.x + c2.x; c3.y = c1.y + c2.y; return (c3); public void display() Console.Write(x); Console.Write(" +j" + y); Console.WriteLine(); class Program { static void Main(string[] args) complex a, b, c; a = new complex(2.5, 3.5); b = new complex(1.5, 0.5); Console.Write("a="); a.display(); Console.Write("b="); b.display(); c = a + b; Console.Write("c="); c.display(); Console.Read(); }

11 Overloading Comparison Operator

12 C# supports six comparison in three pair
== and ! = > and < = < and >= The significance of pairing is : Within each pair, second operator should always give exactly the opposite result to the first That is, whenever first returns true, the second return false C# always requires us to overload the comparison operator in pair, means if we overload = =, than we must overload ! = also, otherwise it is an error

13 Cont.. There is fundamental difference between overloading comparison operator and arithmetic operator Comparison operator must return bool type value

14 class test { int a, b, c; public test() { } public test(int x, int y, int z) a = x; b = y; c = z; } public static bool operator ==(test t1, test t2) if (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c) return (true); else return (false); public static bool operator !=(test t1, test t2) return (!(t1 == t2)); class Program { static void Main(string[] args) test t1 = new test(10, 20, 30); test t2 = new test(10, 20, 30); if (t1 == t2) Console.WriteLine("equal"); else Console.WriteLine("Not Equal"); Console.Read(); }


Download ppt "Operator Overloading."

Similar presentations


Ads by Google