title: Preventing Object Slicing - C++
body: <p>I am trying to create a vector of Person objects.  However I really want to store Adult, Professor, and Student objects which are derived from the Person base class.  After asking <a href="https://stackoverflow.com/questions/8348939/virtual-method-not-calling-dervied-class-method-c">this question</a> I have learned that my issue was in Object Splicing and that it would save the derived classes as a Person class because that is what the vector is of.  I have attempted to modify the code to rectify the situation however I still am coming across some errors and need some additional assistance. Thanks!</p>
  
  <p>Here is my main.cpp code:</p>
  
  <pre><code>#include "Person.h"
  #include "Professor.h"
  #include "Student.h"
  #include "Adult.h"
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  template&lt;typename T&gt; void addPerson(vector&lt;Person *&gt; &amp;personVector) {
      cout &lt;&lt; "DEBUG" &lt;&lt; endl;
      personVector.push_back(new T());
  }
  
  void addFriend(vector&lt;Person *&gt; &amp;personVector) {
      bool loop = true;
      while (loop) {
          cout &lt;&lt; "\nWhich Person would you like to create? " &lt;&lt; endl;
          cout &lt;&lt; "  1. Professor" &lt;&lt; endl;
          cout &lt;&lt; "  2. Student" &lt;&lt; endl;
          cout &lt;&lt; "  3. Adult" &lt;&lt; endl;
          int caseVar;
          cin &gt;&gt; caseVar;
          switch (caseVar) {
          case 1: 
              addPerson&lt;Professor&gt;(personVector);
              // old incorrect line: addPerson&lt;Professor *&gt;(personVector);
              loop = false;
              break;
          case 2: 
              addPerson&lt;Student&gt;(personVector);
              // old incorrect line: addPerson&lt;Student *&gt;(personVector);
              loop = false;
              break;
          case 3: 
              addPerson&lt;Adult&gt;(personVector); 
              // old incorrect line: addPerson&lt;Adult *&gt;(personVector);
              loop = false;
              break;
          default: cout &lt;&lt; "Unknown Entry Please Try Again." &lt;&lt; endl;
          }
      }
  }
  
  void displayFriends(vector&lt;Person *&gt; &amp;personVector) {
      if (personVector.size() &gt; 1) {
          for (unsigned i = 0; personVector.size() &gt; i; i++)
              cout &lt;&lt; i+1 &lt;&lt; ". " &lt;&lt; personVector[i]-&gt;getName() &lt;&lt; endl;
              cout &lt;&lt; "DEBUG" &lt;&lt; endl;
      }
  }
  
  void viewFriend(vector&lt;Person *&gt; &amp;personVector) {
      if (personVector.size() &gt; 1) {
          displayFriends(personVector);
          cout &lt;&lt; "Which # friend would you like to view? ";
          int num;
          cin &gt;&gt; num;
          personVector[num-1]-&gt;coutPerson();
      } else if (personVector.size() == 1) {
          personVector[0]-&gt;coutPerson();
      } else {
          cout &lt;&lt; "No friends to View." &lt;&lt; endl;
      }
  }
  
  
  void deleteElementFromArray(int element, vector&lt;Person *&gt; &amp;personVector) {
      vector&lt;Person *&gt; newPersonVector;
      for (unsigned i = 0; personVector.size() &gt; i; i++)
          if (i != element - 1)
              newPersonVector.push_back(personVector[i]);
      personVector = newPersonVector;
  }
  
  void removeFriend(vector&lt;Person *&gt; &amp;personVector) {
      if (personVector.size() &gt; 1) {
          displayFriends(personVector);
          cout &lt;&lt; "Which # friend would you like to remove? ";
          unsigned num;
          cin &gt;&gt; num;
          if (num &lt;= personVector.size())
              deleteElementFromArray(num, personVector);
          else
              cout &lt;&lt; "Invalid Selection" &lt;&lt; endl;
      } else if (personVector.size() == 1) {
          cout &lt;&lt; "Removed one and only friend" &lt;&lt; endl;
      } else {
          cout &lt;&lt; "No friends to Remove." &lt;&lt; endl;
      }
  }
  
  
  int main() {
      vector&lt;Person *&gt; personVector;
      // Run Main Menu
      cout &lt;&lt; "Friends with Stuff" &lt;&lt; endl;
      cout &lt;&lt; "Adding 5 friends to the list" &lt;&lt; endl;
      personVector.push_back(new Person("James"));
      personVector.push_back(new Person("Martin"));
      personVector.push_back(new Person("Sammi"));
      personVector.push_back(new Person("Donny"));
      personVector.push_back(new Person("Ronald"));
      bool loop = true;
      while (loop) {
          cout &lt;&lt; "\nWhat would you like to do? " &lt;&lt; endl;
          cout &lt;&lt; "  1. Add New Friend" &lt;&lt; endl;
          cout &lt;&lt; "  2. View Friend" &lt;&lt; endl;
          cout &lt;&lt; "  3. Remove Friend" &lt;&lt; endl;
          cout &lt;&lt; "  4. Clear Friends" &lt;&lt; endl;
          cout &lt;&lt; "  5. Get Object" &lt;&lt; endl;
          cout &lt;&lt; "  6. Exit" &lt;&lt; endl;
          int caseVar;
          cin &gt;&gt; caseVar;
          switch (caseVar) {
          case 1: 
              addFriend(personVector);
              break;
          case 2: 
              viewFriend(personVector);
              break;
          case 3: 
              removeFriend(personVector);
              break;
          case 4: 
              personVector.clear();
              break;
          case 5:
  
              break;
          case 6: 
              loop = false; 
              break;
          default: cout &lt;&lt; "Unknown Entry Please Try Again." &lt;&lt; endl;
          }
      }
  }
  </code></pre>
  
  <p>I am getting the following errors</p>
  
  <pre><code>InitializeBuildStatus:
  1&gt;  Touching "Debug\FPVisualStudio.unsuccessfulbuild".
  1&gt;ClCompile:
  1&gt;  main.cpp
  1&gt;\\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(12): error C2664: 'void std::vector&lt;_Ty&gt;::push_back(_Ty &amp;&amp;)' : cannot convert parameter 1 from 'Professor **' to 'Person *&amp;&amp;'
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=Person *
  1&gt;          ]
  1&gt;          Reason: cannot convert from 'Professor **' to 'Person *'
  1&gt;          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  1&gt;          \\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(26) : see reference to function template instantiation 'void addPerson&lt;Professor*&gt;(std::vector&lt;_Ty&gt;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=Person *
  1&gt;          ]
  1&gt;\\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(12): error C2664: 'void std::vector&lt;_Ty&gt;::push_back(_Ty &amp;&amp;)' : cannot convert parameter 1 from 'Student **' to 'Person *&amp;&amp;'
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=Person *
  1&gt;          ]
  1&gt;          Reason: cannot convert from 'Student **' to 'Person *'
  1&gt;          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  1&gt;          \\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(30) : see reference to function template instantiation 'void addPerson&lt;Student*&gt;(std::vector&lt;_Ty&gt;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=Person *
  1&gt;          ]
  1&gt;\\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(12): error C2664: 'void std::vector&lt;_Ty&gt;::push_back(_Ty &amp;&amp;)' : cannot convert parameter 1 from 'Adult **' to 'Person *&amp;&amp;'
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=Person *
  1&gt;          ]
  1&gt;          Reason: cannot convert from 'Adult **' to 'Person *'
  1&gt;          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  1&gt;          \\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(34) : see reference to function template instantiation 'void addPerson&lt;Adult*&gt;(std::vector&lt;_Ty&gt;)' being compiled
  1&gt;          with
  1&gt;          [
  1&gt;              _Ty=Person *
  1&gt;          ]
  1&gt;
  1&gt;Build FAILED.
  1&gt;
  1&gt;Time Elapsed 00:00:02.90
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>I believe that it is telling me that I have too many pointers in my code.  However I don't understand why this would be the case.  The only places I have pointers are saying that the vector contains  which I need to repeat in each case.  And these pointers are needed so I don't have object slicing.</p>
  
  <p>Person.h</p>
  
  <pre><code>#ifndef PERSON_H
  #define PERSON_H
  #include &lt;string&gt;
  #include "Date.h"
  #include "PhoneNumber.h"
  using namespace std;
  
  class Person
  {
  protected:
      string name;
      Date birthday;
      PhoneNumber phoneNumber;
      string city, state;
  public:
      // Constructors
      Person();
      Person(string name) { 
          this-&gt;name = name; 
          birthday = Date();
          phoneNumber = PhoneNumber("0000000000");
          city = "Unknown";
          state = "Unknown";
      }
      // Getter and Setter Methods
      string getName();
      void setName(string);
      Date getBirthday();
      // Methods
      void coutPerson();
  };
  #endif
  </code></pre>
  
  <p>Person.cpp</p>
  
  <pre><code>#include "Person.h"
  #include "PhoneNumber.h"
  #include "Date.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  // Constructors
  Person::Person() {
      cin.ignore();
      cout &lt;&lt; "Name? ";
      getline(cin, name);
      cout &lt;&lt; "Birthday: " &lt;&lt; endl;
      birthday.askUserForDate();
      phoneNumber.create();
      cout &lt;&lt; "City? ";
      getline(cin, city);
      cout &lt;&lt; "State? ";
      getline(cin, state);
  }
  
  // Getter and Setter Methods
  string Person::getName() {
      return name;
  }
  
  void Person::setName(string name) {
      this-&gt;name = name;
  }
  
  void Person::coutPerson() {
      cout &lt;&lt; name &lt;&lt; endl;
      birthday.coutDate();
      phoneNumber.coutPhoneNumber();
      cout &lt;&lt; city &lt;&lt; ", " &lt;&lt; state &lt;&lt; endl;
  }
  
  // Methods
  </code></pre>
  
  <p>Student.h</p>
  
  <pre><code>#ifndef STUDENT_H
  #define STUDENT_H
  #include "Person.h"
  using namespace std;
  
  class Student : public Person
  {
  private:
      string dorm;
      int dormRoom;
  public:
      // Constructors
      Student();
      Student(Student &amp;);
      // Getter and Setter Methods
  
      // Methods
      void coutPerson();
  };
  #endif
  </code></pre>
  
  <p>Student.cpp</p>
  
  <pre><code>#include "Student.h"
  #include "Person.h"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  // Constructors
  Student::Student() {
      cin.ignore();
      cout &lt;&lt; "Dorm? ";
      getline(cin, dorm);
      cout &lt;&lt; "Dorm Room #? ";
      cin &gt;&gt; dormRoom;
  }
  
  // TODO Copy Constructors
  // TODO Deconstructors
  // Overload &lt; &gt; ==
  
  Student::Student(Student &amp;student) {
      Person::Person(student);
      dorm = student.dorm;
      dormRoom = student.dormRoom;
  }
  
  void Student::coutPerson() {
      cout &lt;&lt; "DEBUG: Student::coutPerson()" &lt;&lt; endl;
      Person::coutPerson();
      cout &lt;&lt; "Dorm Room: " &lt;&lt; this-&gt;dorm &lt;&lt; " " &lt;&lt; this-&gt;dormRoom &lt;&lt; endl;
  }
  
  // Methods
  </code></pre>