title: how cout can call the private members of a class
body: <p>I was experimenting with friend function and written following:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class Comp {
  private:
    double r,i;
  public:
    Comp(){
      r = 10.5;
      i = 25.1;
    }
    friend ostream&amp; operator &lt;&lt;(ostream&amp; write, Comp &amp; comp);
  };
  
  ostream&amp; operator &lt;&lt; (ostream&amp; write, Comp&amp; comp){
    write &lt;&lt; comp.r &lt;&lt; ":  " &lt;&lt; comp.i;
    return write;
  }
  
  int main(int argc, char const *argv[]) {
    /* code */
    Comp comp;
    cout &lt;&lt; comp;      
    return 0;
  }
  </code></pre>
  
  <p>so a question arises (as I am not expert in c++ just started learning c++) that why cout can access the private members of class Comp. Because cout is not declared in the class neither it is friend of class. So what mechanism allows to access private members of a class by cout which is object of ostream class.</p>
  
  <p>Basically what I am trying to ask is how an object of other class (object of ostream class cout) can access the private variable of other class Comp without being friend or member of the class.
  thanks.</p>