title: Operator overloading complex numbers - additional function
body: <p>I'm trying to understand the idea of operator overloading, I'm getting to the point when it's all coming together and I might get the the hang of it, but... </p>
  
  <p>I'm struggling to understand some piece of code which uses + operator, which has been overloaded so that it will add complex numbers, the code is pretty straightforward. </p>
  
  <pre><code>#include &lt;iostream&gt; 
  
  using namespace std; 
  
  
  class complex {
    protected:
    double Re, Im;
  
  public:
    complex() : Re(0.0), Im(0.0) {} 
    complex(double Re, double Im) : Re(Re), Im(Im) {} 
    double getRe() const { return Re; }
    double getIm() const { return Im; }
    friend complex operator+(const complex&amp;, const complex&amp;); 
    friend ostream&amp; operator&lt;&lt;(ostream&amp;, const complex&amp;); 
  };
  
  complex operator+(const complex&amp; a, const complex&amp; b) {
  double r, i;
  r = a.getRe() + b.getRe();
  i = a.getIm() + b.getIm();
  **return complex(r, i);** // *** CAN ANYONE EXPLAIN THIS BIT ? ***
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const complex &amp;a) {
    out &lt;&lt; "(" &lt;&lt; a.getRe() &lt;&lt; ", " &lt;&lt; a.getIm() &lt;&lt; ")" &lt;&lt; endl;
    return out;
  }
  
  int main(void) {
    complex a(1,2), b(3,4), c;
    c = a+b;
    cout &lt;&lt; c &lt;&lt; endl;
  </code></pre>
  
  <p>}</p>
  
  <p>I can't understand what exactly line 23 does: </p>
  
  <pre><code>return complex(r, i); 
  </code></pre>
  
  <p>Overloaded adding operator supposed to return some value, as it has been clarified it supposed to be value of complex type. </p>
  
  <p>So that it will look like declared in complex class, <strong>but does it have something to do with constructor ?</strong> </p>
  
  <p>I mean how the compilator will now, what is the type 'complex' look like, since it's a name of a class, does it have something to do with class' constructor ? </p>
  
  <p>Do constructors define the values which are the type of a class ? </p>
  
  <p>For instance, we have class cold dog. </p>
  
  <p>the value that will a type of a class called dog, would be like this: </p>
  
  <p>dog value </p>
  
  <p><strong>Does constructor within dog class defines the definition of value, how it looks like, what information does it store ?</strong> </p>
  
  <ol start="2">
  <li>How can I overload ostream operator >> (cin) so that the user can specify its own values of complex numbers ? </li>
  </ol>
  
  <p>Thanks !</p>