title: How to write copy constructor for Class with object of another class
body: <p>I am having trouble writing this fairly simple program. I have two class A and B. B has an object of A. I need to write Copy constructor of B so that two instances of B will have the different instance of A. Is there any neat way to do this? One thing to do is getting all the member variable of parm, creating a new A object and assigning those member variables. But if the class is having more member variables then it is a problem. How to write this in a simple way?</p>
  
  <pre><code>class A
  {   
  public:
      int data;
      A()
      {
  
      }
      A(int parm) : data(parm)
      {
  
      }
  
      A(const A&amp;parm)
      {
          this-&gt;data = parm.data;
      }
      A&amp; operator = (const A&amp; parm)
      {
          if (this != &amp;parm)
          {
              this-&gt;data = parm.data;
          }
          return *this;
      }
      ~A()
      {
          cout &lt;&lt; "A is destroyed";
      }
  
  };
  class B
  {
  public:
  
      A *a;
      B() 
      {
          a = new A(10);
      }
  
      B(const B&amp;parm)
      {
          // How to copy the value of parm so this and parm have different A object
          // this.a = parm.a --&gt; both this and parm points to same A object
      }
  
      B&amp; operator = (const B&amp;parm)
      {
          if (this != &amp;parm)
          {
              this-&gt;a = parm.a;
          }
          return *this;
      }
  
      ~B()
      {
          // Null check 
          delete a;
      }
  };
  </code></pre>