title: C++ object behavior cannot predict
body: <p>I was trying to understand the following two codes. I used Visual Studio 2017 to compile and run the programs but I cannot understand how the code is executed.</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class A
  {
  public:
    A()
    {
      a[0] = 1;
      a[1] = 0;
    }
    int a[2];
    int b(void)
    {
      int x = a[0];
      a[0] = a[1];
      a[1] = x;
      return x;
    }
  };
  
  int main() {
    A a;
    a.b();
    cout &lt;&lt; a.b() &lt;&lt; a.a[1] &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  <p>My expected result is <code>00</code>.
  But when I run the program in Visual Studio 2017, the answer is <code>01</code>.</p>
  <p>Also, when I separate <code>cout</code> for two lines as follows, the answer is <code>00</code>.</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  class A
  {
  public:
    A()
    {
      a[0] = 1;
      a[1] = 0;
    }
    int a[2];
    int b(void)
    {
      int x = a[0];
      a[0] = a[1];
      a[1] = x;
      return x;
    }
  };
  
  int main() {
    A a;
    a.b();
    cout &lt;&lt; a.b();
    cout &lt;&lt; a.a[1] &lt;&lt; endl;
    return 0;
  }
  </code></pre>
  <p>I tried several online compilers, and both of the above codes give <code>00</code> as the answer.
  Can someone please explain what is the reason for this?</p>