The task
Implement the class in accordance with the variant of the task. The class must contain:

  1. closed immutable fields that store the state of the class;
  2. methods for performing operations on class objects. These methods must apply the operation to the current object (this) and the object passed as the argument of the method. To present the result, a new object is created, which is returned from the method. In this case, neither the current object (this) nor the object argument of the method are changed;
  3. properties that return attributes of the abstraction represented by the class;
  4. private constructor, taking arguments - field values;
  5. static construction methods.
    Instances of this class must be immutable. The console application is required to demonstrate the use of the developed class.

Need to create
Class: Complex number
State (fields): real and imaginary parts
Design methods: the creation of a complex number in algebraic form, the creation of a complex number in trigonometric form
Properties: real part, imaginary part, module, argument
Operations: addition and subtraction

Here is my code

using System; namespace Complex1 { class Program { static void Main(string[] args) { double re, im, mod, arg; Console.Write("Введите действительную часть комплексного числа re = "); re = Convert.ToDouble(Console.ReadLine()); Console.Write("Введите мнимую часть комплексного числа im = "); im = Convert.ToDouble(Console.ReadLine()); Complex res = new Complex(re, im); } } public class Complex { public Complex() { } public Complex(double _re, double _im) { re = _re; im = _im; } public Complex(double _mod, double _arg) { re = _mod * Math.Cos(_arg); im = _mod * Math.Sin(_arg); } public static Complex operator +(Complex num1, Complex num2) { return new Complex(num1.re + num2.re, num2.im + num2.im); } public static Complex operator -(Complex num1, Complex num2) { return new Complex(num1.re - num2.re, num2.im - num2.im); } public double Re { get; set; } public double Im { get; set; } private double re, im; } } 

The first question - I got two constructors with the same signatures, how to solve it?
And I do not understand how to apply p2. methods for performing operations on class objects. These methods must apply the operation to the current object (this) and the object passed as the argument of the method. To present the result, a new object is created, which is returned from the method. In this case, neither the current object (this) nor the object argument of the method are changed; It seems that the use of the keyword this in me is not necessary anywhere.

and p5. Why do I need static constructors here?

  • @VladD why do I get in this code pastebin.com/80WGPDsZ that the complex numbers are zeros? - Heidel
  • one
    @Heidel: Because you got the properties of Re , Im , and the fields re , im . You initialize only the fields, and the properties both were zeros and remain. Throw out the fields completely, and for the properties prohibit the modification from the outside: public double Re {get; private set; } - VladD am
  • And if I, on assignment in the class, should have закрытые неизменяемые поля, хранящие состояние класса; ? - Heidel
  • one
    @Heidel: then: private readonly double re; public double Re {get {return re; }} --- Probably, also like this: public double Mod {get {return Math.Sqrt (re * re + im * im); }} public double Arg {get {return Math.Atan2 (im, re); }} - VladD
  • Thank you for help! - Heidel

1 answer 1

For the same constructors: apply the factory method!

 public class Complex { public Complex() { } private Complex(double _re, double _im) { re = _re; im = _im; } public static Complex FromCartesian(double _re, double _im) { return new Complex(_re, _im); } public static Complex FromPolar(double _mod, double _arg) { var re = _mod * Math.Cos(_arg); var im = _mod * Math.Sin(_arg); return new Complex(re, im); } // ... 

You basically wrote the addition operator correctly, but the task requires the use of a non-static method, so try this:

  public Complex operator + (Complex num2) { return new Complex(this.re + num2.re, this.im + num2.im); } 

By the way, you had a typo there: instead of num2.im + num2.im you would need num1.im + num2.im (and the same thing in the subtraction).

Update: overloaded addition operators should be static, this will not work. Either leave it as it is, or replace operator + with the non-static Add method.

In Section 5, you do not have static constructors, but static construction methods . Here we mean just methods of type FromPolar : they are static, and create an object, that is, they are used instead of a constructor. (This is also called the “named constructor idiom”).

  • Can you explain how this works? - Heidel
  • Actually, it doesn't work. - Heidel
  • one
    @Heidel: “it does not work” is a useless description of the problem. Describe what you are doing, what you expect, and what you see in reality. - VladD
  • Your addition operator does not work public static Complex operator +(Complex num2) { return new Complex(this.re + num2.re, this.im + num2.im); } public static Complex operator +(Complex num2) { return new Complex(this.re + num2.re, this.im + num2.im); } Can't use 'this' in static methos - Heidel
  • 2
    @Heidel: And really, this is not the case. Then either leave static with two arguments, or make a method from this: public Complex Add (Complex that) {return new Complex (this.re + that.re, this.im + that.im); } (but then you cannot use addition with the + character). Regarding non-static methods, it is said in the assignment:> These methods should apply the operation to the current object (this) and the object passed as an argument to the method - VladD