If you're an OOP programmer, you know that you can also overload operators, not just methods . You do that by defining ## Table 3.3. Overloading Possibilities for C# Operators
Note also that, unlike C++, the You can overload operators for either classes or structs. To see how this works, we'll overload the
## Listing 3.14 Overloading Operators (ch03_14.cs)class ch03_14 { public static void Main() { Complex complex1 = new Complex(1, 2); Complex complex2 = new Complex(3, 4); System.Console.WriteLine("complex1 = {0}", complex1); System.Console.WriteLine("complex2 = {0}", complex2); Complex complex3 = -complex1; System.Console.WriteLine("-complex1 = {0}", complex3); System.Console.WriteLine("complex1 + complex2 = {0}", complex1 + complex2); if(complex1 == complex2){ System.Console.WriteLine("complex1 equals complex2"); } else { System.Console.WriteLine("complex1 does not equal complex2"); } } } public struct Complex { public int real; public int imaginary; public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } public override string ToString() { if (imaginary >= 0){ return(System.String.Format("{0} + {1}i", real, imaginary)); } else { return(System.String.Format("{0} - {1}i", real, System.Math.Abs(imaginary))); } } public static Complex operator-(Complex complex) { return new Complex(-complex.real, -complex.imaginary); } public static Complex operator+(Complex complex1, Complex complex2) { return new Complex(complex1.real + complex2.real, complex1.imaginary + complex2.imaginary); } public static implicit operator Complex(int theInt) { return new Complex(theInt, 0); } public static explicit operator int(Complex complex) { return complex.real; } public static bool operator==(Complex complex1, Complex complex2) { if (complex1.real == complex2.real && complex1.imaginary == complex2.imaginary) { return true; } return false; } public static bool operator!=(Complex complex1, Complex complex2) { return !(complex1 == complex2); } public override bool Equals(object obj) { if (!(obj is Complex)) { return false; } return this == (Complex) obj; } public override int GetHashCode() { return (int) System.Math.Sqrt(real * real + imaginary * imaginary); } } ## Creating the Complex Struct We start ch03_14.cs by using the public struct Complex { public int real; public int imaginary; public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } public override string ToString() { if (imaginary >= 0){ return(System.String.Format("{0} + {1}i", real, imaginary)); } else { return(System.String.Format("{0} - {1}i", real, System.Math.Abs(imaginary))); } } . . . } ## Overloading a Unary Operator The next step is to start overloading operators for public static Complex operator-(Complex complex) { return new Complex(-complex.real, -complex.imaginary); }
All we have to do here is to negate the real and imaginary parts of the complex number and return the result, as you see in this code. Now if you created a complex number, 1 + 2i, and negated it like this: Complex complex1 = new Complex(1, 2); System.Console.WriteLine(-complex1; You'd see this result: -1 - 2i ## Overloading a Binary Operator We've been able to overload the public static Complex operator+(Complex complex1, Complex complex2) { return new Complex(complex1.real + complex2.real, complex1.imaginary + complex2.imaginary); } Now if you were to use this code to add 1 + 2i and 3 + 4i: Complex complex1 = new Complex(1, 2); Complex complex2 = new Complex(3, 4); System.Console.WriteLine("complex1 + complex2 = {0}", complex1 + complex2); you would see this result: complex1 + complex2 = 4 + 6i ## Overloading Conversion Operations You can also overload conversion operations. Conversions can be either implicit or explicit, and you use the public static implicit operator Complex(int intValue) { return new Complex(intValue, 0); }
On the other hand, converting from a complex number to an public static explicit operator int(Complex complex) { return complex.real; } Now when you cast from ## Overloading Equality Operators Overloading the When you overload the public static bool operator==(Complex complex1, Complex complex2) { if (complex1.real == complex2.real && complex1.imaginary == complex2.imaginary) { return true; } return false; }
And here's how to overload public static bool operator!=(Complex complex1, Complex complex2) { return !(complex1 == complex2); } If you only overload public override bool Equals(object obj) { if (!(obj is Complex)) { return false; } return this == (Complex) obj; } But there's still more to do. If you've overloaded public override int GetHashCode() { return (int) System.Math.Sqrt(real * real + imaginary * imaginary); } Now, at last, you can compare two complex numbers using the Complex complex1 = new Complex(1, 2); Complex complex2 = new Complex(3, 4); if(complex1 == complex2){ System.Console.WriteLine("complex1 equals complex2"); } else { System.Console.WriteLine("complex1 does not equal complex2"); } Here's what this code produces: complex1 does not equal complex2 For the full story on operator overloading, run ch03_14.cs; this example implements all the operator overloads we've discussed and puts them to work using the code we've developed. Here's what you see when you run this example: C:\>ch03_14 complex1 = 1 + 2i complex2 = 3 + 4i -complex1 = -1 - 2i complex1 + complex2 = 4 + 6i complex1 does not equal complex2 |

Microsoft Visual C#.NET 2003 Kick Start

ISBN: 0672325470

EAN: 2147483647

EAN: 2147483647

Year: 2002

Pages: 181

Pages: 181

Authors: Steven Holzner

Similar book on Amazon

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net