This post demonstrates how to define your own operators by using user-defined classes that can overload operators .
The following example changes the behaviour of built-in addition operator. Let name this user-defined operator as “Complex Addition”. Consider a complex number, num = 2+3i, where 2 is a real part and 3i is an imaginary part. So the complex addition operator sums the real parts and imaginary parts of two complex numbers.
Please share your thoughts on this post in the comments section.
Operator overloading allows user to define own operator implementations to be specified for operations where the operands are of user-defined or struct type, thus changing their meaning when applied.
The following example changes the behaviour of built-in addition operator. Let name this user-defined operator as “Complex Addition”. Consider a complex number, num = 2+3i, where 2 is a real part and 3i is an imaginary part. So the complex addition operator sums the real parts and imaginary parts of two complex numbers.
Code -
// complex.cs using System; public struct Complex { public int real; public int imaginary; //parametrized constructor public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } // Declare which operator to overload (+), the types // that can be added (two Complex objects), and the // return type (Complex): public static Complex operator +(Complex c1, Complex c2) { return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary); } // Override the ToString method to display an complex number in the suitable format: public override string ToString() { return(String.Format("{0} + {1}i", real, imaginary)); } public static void Main() { Complex num1 = new Complex(2, 3); Complex num2 = new Complex(3, 4); // Add two Complex objects (num1 and num2) through the // overloaded plus operator: Complex sum = num1 + num2; // Print the numbers and the sum using the overriden ToString method: Console.WriteLine("First complex number: {0}", num1); Console.WriteLine("Second complex number: {0}", num2); Console.WriteLine("The sum of the two numbers: {0}", sum); } }
/* output: First complex number: 2 + 3i Second complex number: 3 + 4i The sum of the two numbers: 5 + 7i */
Please share your thoughts on this post in the comments section.
0 comments:
Post a Comment