title: Getting the same string (gene array) in every object in the array dna (dynamically allocated)
body: <p>I have created a class called DNA, having a no argument constructor and two member functions namely initialize() and show(). The problem is when I create an
  array using new operator and call the initialize function on every object using a for loop, instead of getting different string in the member variable "genes", I am getting the exactly the same set of characters (array) in the genes in every object in the array. Although I seed the srand() function before initialization of the string, there is no effect seen of it.</p>
  
  <p>The code below.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include &lt;string&gt;
  using namespace std;
  
  string sampleSpace("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz");
  
  class DNA {
  private:
      int length;
      char *genes;
  
  public:
      DNA() {
          length = 0;
          genes = new char[length];
      }
  
      void initialize(int len) {
          srand(unsigned(time(NULL)));
          this -&gt; length = len;
          delete genes;
          this -&gt; genes = new char[length];
  
          for (int i = 0; i &lt; length; i++) {
              *(genes + i) = sampleSpace.at(rand() % sampleSpace.length());
          }
      }
  
      void show() {
          for (int i = 0; i &lt; length; i++) {
              cout&lt;&lt;*(genes + i);
          }
          cout&lt;&lt;endl;
      }
  };
  
  int main() {
      DNA *dna = new DNA[10];
      DNA *temp = dna;
      for (int i = 0; i &lt; 10; i++) {
          (*temp).initialize(10);
          temp++;
      }
      temp = dna;
      for (int i = 0; i &lt; 10; i++) {
          (*temp).show();
          temp++;
      }
      return 0;
  }
  </code></pre>