// Exercise 3g from Lecture 3 using System; // public class Complex { public class Complex { // explicit fields: //public int Real; //public int Imag; // automatic properties: public int Real { get ; /* set ; */ } public int Imag { get ; /* set ; */ } public Complex () {} public Complex (int r, int i) { this.Real = r; this.Imag = i; } public override string ToString() { return this.Real.ToString()+"+"+this.Imag.ToString()+"i"; } public static Complex operator+ (Complex a, Complex b) { return new Complex(a.Real + b.Real, a.Imag + b.Imag); } } public class Tester { public static void Main (string []args) { if (args.Length != 4) { // expect 4 args: r1 i1 r2 i2 System.Console.WriteLine("Usage: exercise3g "); } else { int r1 = Convert.ToInt32(args[0]); int i1 = Convert.ToInt32(args[1]); int r2 = Convert.ToInt32(args[2]); int i2 = Convert.ToInt32(args[3]); { Complex c1 = new Complex(r1,i1); Complex c2 = new Complex(r2,i2); System.Console.WriteLine("Complex addition: {0} + {1} = {2}", c1, c2, c1+c2); } { Complex c1 = new Complex(1,2); Complex c2 = new Complex(3,4); System.Console.WriteLine("Complex addition: {0} + {1} = {2}", c1, c2, c1+c2); } { Complex c1 = new Complex(); Complex c2 = new Complex(); c1.Real = 1; c1.Imag = 2; c2.Real = 3; c2.Imag = 4; System.Console.WriteLine("Complex addition: {0} + {1} = {2}", c1, c2, c1+c2); } { Complex c1 = new Complex(); Complex c2; c1.Real = 1; c1.Imag = 2; c2 = c1; c2.Real = 3; c2.Imag = 4; System.Console.WriteLine("%%% Complex addition: {0} + {1} = {2}", c1, c2, c1+c2); } } } }