Your SlideShare is downloading. ×
0
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Operator overloading
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Operator overloading

169

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
169
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Operator Overloading • C# allows you to define the meaning of an operator relative to a class that you create. This process is called operator overloading. • A principal advantage of operator overloading is that it allows you to seamlessly integrate a new class type into your programming environment. • Once operators are defined for a class, you can operate on objects of that class using the normal C# expression syntax. • You can even use an object in expressions involving other types of data.
  • 2. • Operator overloading is closely related to method overloading. To overload an operator, use the operator keyword to define an operator method, which defines the action of the operator relative to its class. There are two forms of operator methods: • one for unary operators and • one for binary operators.
  • 3. // General form for overloading a unary operator public static ret-type operator op (param-type operand) { // operations } // General form for overloading a binary operator public static ret-type operator op (param-type1 operand1, param-type1 operand2 ) { // operations }
  • 4. • the operator that you are overloading, such as + or /, is substituted for op . • The ret-type specifies the type of value returned by the specified operation. • the return value is often of the same type as the class for which the operator is being overloaded. • For unary operators, the operand is passed in operand. For binary operators, the operands are passed in operand1 and operand2. • Operator methods must be both public and static.
  • 5. • For unary operators, the operand must be of the same type as the class for which the operator is being defined. • For binary operators, at least one of the operands must be of the same type as its class. • Operator parameters must not use the ref or out modifier.
  • 6. class ThreeD { int x, y, z; public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } public static ThreeD operator +(ThreeD op1, ThreeD op2) { ThreeD result = new ThreeD(); result.x = op1.x + op2.x; result.y = op1.y + op2.y; result.z = op1.z + op2.z; return result; }
  • 7. public static ThreeD operator -(ThreeD op1, ThreeD op2) { ThreeD result = new ThreeD(); result.x = op1.x - op2.x; result.y = op1.y - op2.y; result.z = op1.z - op2.z; return result; } public void Show() { Console.WriteLine(x + ", " + y + ", " + z); }
  • 8. class ThreeDDemo { static void Main() { ThreeD a = new ThreeD(1, 2, 3); ThreeD b = new ThreeD(10, 10, 10); ThreeD c; Console.Write("Here is a: "); a.Show(); Console.Write("Here is b: "); b.Show();
  • 9. c = a + b; Console.Write("Result of a + b: "); c.Show(); c = a + b + c; // add a, b, and c together Console.Write("Result of a + b + c: "); c.Show(); c = c - a; Console.Write("Result of c - a: "); c.Show(); c = c - b; Console.Write("Result of c - b: "); c.Show(); }
  • 10. output Here is a: 1, 2, 3 Here is b: 10, 10, 10 Result of a + b: 11, 12, 13 Result of a + b + c: 22, 24, 26 Result of c - a: 21, 22, 23 Result of c - b: 11, 12, 13
  • 11. Overload unary(- or ++) class ThreeD { int x, y, z; public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } public static ThreeD operator -(ThreeD op) { ThreeD result = new ThreeD(); result.x = -op.x; result.y = -op.y; result.z = -op.z; return result; }
  • 12. public static ThreeD operator ++(ThreeD op) { ThreeD result = new ThreeD(); result.x = op.x + 1; result.y = op.y + 1; result.z = op.z + 1; return result; } public void Show() { Console.WriteLine(x + ", " + y + ", " + z); }}
  • 13. class ThreeDDemo { static void Main() { ThreeD a = new ThreeD(1, 2, 3); ThreeD b = new ThreeD(10, 10, 10); ThreeD c = new ThreeD(); c = -a; // assign -a to c Console.Write("Result of -a: "); c.Show(); c = a++; // post-increment a Console.WriteLine("Given c = a++"); Console.Write("c is "); c.Show(); Console.Write("a is "); a.Show();
  • 14. Next..
  • 15. The operators that you can overload are:
  • 16. The operators that you can not overload are:
  • 17. Binary with int class ThreeD { int x, y, z; public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } public static ThreeD operator +(ThreeD op1, int op2) { ThreeD result = new ThreeD(); result.x = op1.x + op2; result.y = op1.y + op2; result.z = op1.z + op2;
  • 18. • • • • • • • • • public void Show() { Console.WriteLine(x + ", " + y + ", " + z); }} class ThreeDDemo { static void Main() { ThreeD a = new ThreeD(1, 2, 3); ThreeD b = new ThreeD(10, 10, 10); ThreeD c = new ThreeD();
  • 19. a.Show(); b.Show(); c = b + 10; // ThreeD + int Console.Write("Result of b + 10: "); c.Show();
  • 20. Next..
  • 21. Conversion Operators • C# allows you to create a special type of operator method called a conversion operator. • A conversion operator converts an object of your class into another type. • Conversion operators help fully integrate class types into the C# programming environment by allowing objects of a class to be freely mixed with other data types as long as a conversion to those other types is defined.
  • 22. two forms of conversion operators, implicit and explicit • public static operator implicit target-type (source-type v) { return value; } • public static operator explicit target-type (source-type v) { return value; } • target-type is the target type that you are converting to; source-type is the type you are converting from; and value is the value of the class after conversion.
  • 23. • If the conversion operator specifies implicit , then the conversion is invoked automatically, such as when an object is used in an expression with the target type. • When the conversion operator specifies explicit, the conversion is invoked when a cast is used. • You cannot define both an implicit and explicit conversion operator for the same target and source types.
  • 24. Implicit type conversion using System.Collections; public class TestClass { public void Test(Author a) { Console.WriteLine("Name {0} {1}", a.First, a.Last); } public void Test(Writer w) { Console.WriteLine("Name {0} {1}", w.FirstName, w.LastName);
  • 25. public class Author { public string First; public string Last; public string[] BooksArray; public static implicit operator Writer(Author a) { Writer w = new Writer(); w.FirstName = a.First; w.LastName = a.Last; w.Books = a.BooksArray != null ? a.BooksArray.ToList():null; return w; } }
  • 26. public class Writer { public string FirstName; public string LastName ; public IList Books ; } class Program {
  • 27. static void Main(string[] args) { Author a = new Author { First = "Vijaya", Last = "Anand", BooksArray = new string[] { "book1" } }; Writer w = a; TestClass t = new TestClass(); t.Test(w); t.Test(a); } }
  • 28. an explicit conversion operator, which is invoked only when an explicit cast is used class ThreeD { int x, y, z; public ThreeD() { x = y = z = 0; } public ThreeD(int i, int j, int k) { x = i; y = j; z = k; } public static ThreeD operator +(ThreeD op1, ThreeD op2) { ThreeD result = new ThreeD(); result.x = op1.x + op2.x; result.y = op1.y + op2.y; result.z = op1.z + op2.z; return result;
  • 29. This is now explicit public static explicit operator int(ThreeD op1) { return op1.x * op1.y * op1.z; } public void Show() { Console.WriteLine(x + ", " + y + ", " + z); } }
  • 30. • class ThreeDDemo { • static void Main() { • ThreeD a = new ThreeD(1, 2, 3); • ThreeD b = new ThreeD(10, 10, 10); • ThreeD c = new ThreeD(); • int i; • c = a + b; • Console.Write("Result of a + b: "); • c.Show(); • Console.WriteLine();
  • 31. i = (int) a; // explicitly convert to int -- cast required Console.WriteLine("Result of i = a: " + i); Console.WriteLine(); i = (int)b- (int)a; // casts required Console.WriteLine("result of b-a: " + i);

×