title: class name as return type & use of this pointer
body: <p>I was learning about the <code>this</code> pointer, and I came across the below code. Can anyone explain what the code does? What does <code>*this</code> mean? Why does <code>setX()</code> have a return type as class <code>Test</code>? What is the purpose of placing <code>&amp;</code> before <code>setX()</code> and <code>setY()</code>?</p>
  <pre class="lang-cpp prettyprint-override"><code>#include&lt;iostream&gt; 
  using namespace std; 
    
  class Test 
  { 
  private: 
    int x; 
    int y; 
  public: 
    Test(int x = 0, int y = 0) { this-&gt;x = x; this-&gt;y = y; } 
    Test &amp;setX(int a) { x = a; return *this; } 
    Test &amp;setY(int b) { y = b; return *this; } 
    void print() { cout &lt;&lt; &quot;x = &quot; &lt;&lt; x &lt;&lt; &quot; y = &quot; &lt;&lt; y &lt;&lt; endl; } 
  }; 
    
  int main() 
  { 
    Test obj1(5, 5); 
    
    // Chained function calls.  All calls modify the same object 
    // as the same object is returned by reference 
    obj1.setX(10).setY(20); 
    
    obj1.print(); 
    return 0; 
  } 
  </code></pre>