title: Heterogeneous list and dynamic array allocation
body: <p>So I'm having an issue populating this heterogeneous list. I'm passed in a text file and use it to populate the member data of the objects then add them to the list. This is my first time using this and I can't understand why it's not working properly. It should save each populated objects address to a pointer in the array of pointers, correct? However when I test it I can see that it is only saving to the single location of (*list)[0]. The most relevant portion will probably come from main.cpp. Thank you!</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  #include "student.h"
  
  using namespace std;
  
  int main()
  {
      string inputFileName;
      string outputFileName;
  
      cout &lt;&lt; "Please enter the input file name: ";
      getline(cin, inputFileName);
  
      ifstream inputFile;
      int NUMBEROFENTRIES;
      inputFile.open(inputFileName.c_str());   //CLOSE
      if(inputFile){
          NUMBEROFENTRIES = int(inputFile.get())-int('0');
  
      }else{
          cout &lt;&lt; "Failed to open file." &lt;&lt; endl;
      }
  
      Student ** list; //create a pointer to Student pointer
      list = new Student*[NUMBEROFENTRIES];  //dynamically allocated list of Student pointers
  
      for(int i = 0; i &lt; NUMBEROFENTRIES; i++) 
      {
          string uselessNewLine;
          getline(inputFile, uselessNewLine);
  
          string tempfirstname;
          string templastname;
          getline(inputFile, templastname, ',');
          inputFile.get();
          getline(inputFile, tempfirstname);
  
          char tempcourse = inputFile.get();
          if(tempcourse == 'b'||tempcourse == 'B')
          {
              BioStudent bobj;
  
              bobj.setNameAndCourse(tempfirstname, templastname, tempcourse);
  
              string garbage;
              getline(inputFile, garbage, ' ');
  
              bobj.SetGrades(inputFile);
  
              list[i] = &amp;bobj; //I assume this is where the error is but i should be incrementing?
  
          }
  
          else if(tempcourse == 't' || tempcourse == 'T')
          {
              TheaterStudent tobj;
  
              tobj.setNameAndCourse(tempfirstname, templastname, tempcourse);
  
              string garbage;
              getline(inputFile, garbage, ' ');
  
              tobj.SetGrades(inputFile);
  
              list[i] = &amp;tobj;  //I assume this is where the error is but i should be incrementing?
  
  
          }
  
          else if(tempcourse == 'c' || tempcourse == 'C')
          {
              CompsciStudent cobj;
  
              cobj.setNameAndCourse(tempfirstname, templastname, tempcourse);
  
              string garbage;
              getline(inputFile, garbage, ' ');
              getline(inputFile, garbage, ' ');
  
              cobj.SetGrades(inputFile);
  
              list[i] = &amp;cobj;  //I assume this is where the error is but i should be incrementing?
  
  
  
          }else{
              cout &lt;&lt; "ERROR" &lt;&lt; endl;
          }
  
          cout &lt;&lt; (*list[0]).course &lt;&lt; endl;
  
      }
  
  
  
      delete [] list;
      return 0;
  }
  </code></pre>
  
  <p><strong>student.h</strong></p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  
  class Student
  {
  public:
      virtual double GetAverage()=0;  //Another pure virtual function
      void setNameAndCourse(string fn, string ln, char tc);
      Student();
      char course;
      char GetCourse();
  protected:
  
      string firstName;
      string lastName;
  
  
  private:
  
  };
  
  
  class BioStudent: public Student
  {
  public:
      BioStudent();
      void SetGrades(ifstream &amp;input);
      double GetAverage();
  
  private:
      int labGrade;
      int test1;
      int test2;
      int test3;
      int finalExam;
  
  };
  
  
  class TheaterStudent: public Student
  {
  public:
      TheaterStudent();
      void SetGrades(ifstream &amp;input);
      double GetAverage();
  
  private:
      int participation;
      int midterm;
      int finalExam;
  
  };
  
  
  class CompsciStudent: public Student
  {
  public:
      CompsciStudent();
      void SetGrades(ifstream &amp;input);
      double GetAverage();
  
  private:
      int assign1;
      int assign2;
      int assign3;
      int assign4;
      int assign5;
      int assign6;
      double assignAverage;
      int test1;
      int test2;
      int finalExam;
  };
  </code></pre>
  
  <p><strong>student.cpp</strong></p>
  
  <pre><code>#include "student.h"
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  Student::Student()
  {
      firstName = "";
      lastName = "";
      course = ' ';
  }
  
  void Student::setNameAndCourse(string fn, string ln, char tc)
  {
      firstName = fn;
      lastName = ln;
      course = tc;
  
  }
  
  char Student::GetCourse()
  {
      return course;
  }
  
  BioStudent::BioStudent()
  {
      labGrade = 0;
      test1 = 0;
      test2 = 0;
      test3 = 0;
      finalExam = 0;
  }
  
  void BioStudent::SetGrades(ifstream&amp; input)  
  {
      input &gt;&gt; labGrade;
      input &gt;&gt; test1;
      input &gt;&gt; test2;
      input &gt;&gt; test3;
      input &gt;&gt; finalExam;
  
  }
  
  double BioStudent::GetAverage()
  {
      double toReturn = 0.0;
      toReturn = ((labGrade*.3) + (test1*.15) + (test2*.15) + (test3*.15) + (finalExam*.25));
      return toReturn;
  }
  
  TheaterStudent::TheaterStudent()
  {
      participation = 0;
      midterm = 0;
      finalExam = 0;
  }
  
  void TheaterStudent::SetGrades(ifstream &amp;input) 
  {
      input &gt;&gt; participation;
      input &gt;&gt; midterm;
      input &gt;&gt; finalExam;
  
  }
  
  double TheaterStudent::GetAverage()
  {
      double toReturn = 0.0;
      toReturn = ((participation*.4)+(midterm*.25)+(finalExam*.35));
      return toReturn;
  }
  
  CompsciStudent::CompsciStudent()
  {
      assign1 = 0;
      assign2 = 0;
      assign3 = 0;
      assign4 = 0;
      assign5 = 0;
      assign6 = 0;
      assignAverage = 0.0;
      test1 = 0;
      test2 = 0;
      finalExam = 0;
  }
  
  void CompsciStudent::SetGrades(ifstream &amp;input)
  {
      input &gt;&gt; assign1;
      input &gt;&gt; assign2;
      input &gt;&gt; assign3;
      input &gt;&gt; assign4;
      input &gt;&gt; assign5;
      input &gt;&gt; assign6;
      input &gt;&gt; test1;
      input &gt;&gt; test2;
      input &gt;&gt; finalExam;
  
  }
  
  double CompsciStudent::GetAverage()
  {
      double toReturn = 0.0;
      assignAverage = ((assign1+assign2+assign3+assign4+assign5+assign6)/6);
      toReturn = ((assignAverage*.3)+(test1*.2)+(test2*.2)+(finalExam*.3));
      return toReturn;
  }
  </code></pre>