title: Creating a C++ object with and without new keyword
body: <p>Creating an object with new keyword:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class Person {
    private:
    string name;
  
    public:
    Person(string name) {
      setName(name);
    }
  
    string getName() {
      return this-&gt;name;
    }
  
    void setName(string name) {
      this-&gt;name = name;
    }
  };
  
  int main() {
    Person *person1 = new Person("Rajat");
    Person *person2 = person1;
  
    person2-&gt;setName("Karan");
  
    cout &lt;&lt; person1-&gt;getName() &lt;&lt; endl;
    cout &lt;&lt; person2-&gt;getName() &lt;&lt; endl;
  
    return 0;
  }
  </code></pre>
  
  <p>Output:  </p>
  
  <pre><code>Karan  
  Karan
  </code></pre>
  
  <p>Creating an object without new keyword:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class Person {
    private:
    string name;
  
    public:
    Person(string name) {
      setName(name);
    }
  
    string getName() {
      return this-&gt;name;
    }
  
    void setName(string name) {
      this-&gt;name = name;
    }
  };
  
  int main() {
    Person person1("Rajat");
    Person person2 = person1;
  
    person2.setName("Karan");
  
    cout &lt;&lt; person1.getName() &lt;&lt; endl;
    cout &lt;&lt; person2.getName() &lt;&lt; endl;
  
    return 0;
  }
  </code></pre>
  
  <p>Output:  </p>
  
  <pre><code>Rajat  
  Karan  
  </code></pre>
  
  <p>I expected the output to be 'Karan Karan' as I thought in <code>Person person2 = person1</code>, <code>person2</code> refers to the same <code>person1</code>. But it isn't the case.</p>
  
  <p>Can someone please explain what does the line <code>Person person2 = person1</code> do under the hood? Does it create a totally new object?</p>