Você está na página 1de 3

How to: Use Operator Overloading to Create a Complex Number Class (...

http://msdn.microsoft.com/en-us/library/6fbs5e2h

How to: Use Operator Overloading to Create a Complex Number Class (C# Programming Guide)
Visual Studio 2010 3 out of 5 rated this helpful - Rate this topic This example shows how you can use operator overloading to create a complex number class Complex that defines complex addition. The program displays the imaginary and the real parts of the numbers and the addition result using an override of the ToString method.

Example

public struct Complex { public int real; public int imaginary; // Constructor. public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } // Specify 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 a complex number // in the traditional format: public override string ToString() { return (System.String.Format("{0} + {1}i", real, imaginary)); }

class TestComplex { static void Main() { Complex num1 = new Complex(2, 3); Complex num2 = new Complex(3, 4); // Add two Complex objects by using the overloaded + operator. Complex sum = num1 + num2; // Print the numbers and the sum by using the overridden // ToString method.

1 of 3

5/25/2012 11:43 AM

How to: Use Operator Overloading to Create a Complex Number Class (...

http://msdn.microsoft.com/en-us/library/6fbs5e2h

System.Console.WriteLine("First complex number: {0}", num1); System.Console.WriteLine("Second complex number: {0}", num2); System.Console.WriteLine("The sum of the two numbers: {0}", sum); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey();

} } /* Output: First complex number: 2 + 3i Second complex number: 3 + 4i The sum of the two numbers: 5 + 7i */

See Also
Reference C# Operators operator (C# Reference) Concepts C# Programming Guide Other Resources Why are overloaded operators always static in C#?

Did you find this helpful?

Yes

No

Community Content
More corrections
I agree with Piddu for the first suggestion he made. For the second suggestion, "{0} {1}i" wouldn't be correct.

Following are the ways it will be correct: return (String.Format("{0} {1} {2}i", real, imaginary >= 0 ? '+' : '-', imaginary < 0 ? -imaginary : imaginary)); or

2 of 3

5/25/2012 11:43 AM

How to: Use Operator Overloading to Create a Complex Number Class (...

http://msdn.microsoft.com/en-us/library/6fbs5e2h

return (String.Format("{0} + ({1})i", real, imaginary)); 12/16/2011 Dhruv Rangunwala

Two errors
This code contains two mistakes: first of all, why are the real and imaginary parts of the complex number integers? C is R x R, so they should be double or a similar type. Next, the ToString override prints "{0} + {1}i", but this way you'll be getting results like "5 + -2i", which is, apart from incorrect, really ugly to see :-) It should be changed to "{0} {1}i" 3/8/2011 Piddu 3/8/2011 Piddu

2012 Microsoft. All rights reserved.

3 of 3

5/25/2012 11:43 AM

Você também pode gostar