You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on May 14, 2024. It is now read-only.
publicstructComplex{publicintreal;publicintimaginary;// Constructor.publicComplex(intreal,intimaginary){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).publicstatic Complex operator+(Complexc1,Complexc2){returnnew Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);}// Override the ToString() method to display a complex number// in the traditional format:publicoverridestringToString(){return(System.String.Format("{0} + {1}i", real, imaginary));}}classTestComplex{staticvoidMain(){Complexnum1=new Complex(2,3);Complexnum2=new Complex(3,4);// Add two Complex objects by using the overloaded + operator.Complexsum=num1+num2;// Print the numbers and the sum by using the overridden// ToString method.
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*/