title: Polymorphism in C++ never upcasts
body: <p>I have been a java developer for a long time. Im sure Im thinking of this the wrong way. </p>
  
  <p>But I have a class Animal and a class Dog. Dog extends animal and in my main, I make a bunch of dogs and stick them into a vector of Animals. </p>
  
  <p>While iterating over them, I can not access Dog specific methods on objects that where originally dogs. </p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>Animal</strong></p>
  
  <pre><code> #include "Header.h"
  class Animal {
      std::string name;
      int age;
  public:
      std::string getName(){ return name;}
      virtual int getAge(){ return age;}
      virtual int getRealAge();
      Animal(std::string name, int age);
      ~Animal();
  
  };
  
  Animal::Animal(std::string name, int age){
      this-&gt;name = name;
      this-&gt;age = age;
  }
  
  int Animal::getRealAge() {
      return this-&gt;age*100/2;
  }
  
  Animal::~Animal() {
  
  }
  </code></pre>
  
  <p><strong>Dog</strong></p>
  
  <pre><code>#include "Header.h"
  class Dog : public Animal {
  public :
      Dog(std::string name, int age);
      ~Dog();
      int getAge();
  
  };
  
  Dog::Dog(std::string name, int age) : Animal::Animal(name, age) {
  
  }
  
  Dog::~Dog(){
  
  }
  
  int Dog::getAge() {
      return Animal::getAge()*7;
  }
  </code></pre>
  
  <p><strong>Main</strong></p>
  
  <pre><code>#include "Header.h"
  using namespace std;
  
  int main() {
      cout &lt;&lt; "Hello, World!" &lt;&lt; endl;
      // I make a bunch of Dogs
      Dog tmp0("Ellie", 1);
      Dog tmp5("sam", 2);
      Dog tmp4("lindsay", 3);
      Dog tmp3("shilo", 4);
      Dog tmp2("rex", 5);
      Dog tmp1("bob", 6);
      Animal tmp00("nick", 11);
  
      vector&lt;Animal*&gt; animals;
  
      animals.push_back(&amp;tmp0); // Dog is-an Animal
      animals.push_back(&amp;tmp1);
      animals.push_back(&amp;tmp2);
      animals.push_back(&amp;tmp3);
      animals.push_back(&amp;tmp4);
      animals.push_back(&amp;tmp5);
      animals.push_back(&amp;tmp00); //new
  
      for(auto &amp;animal : animals){
          Animal *tmp = NULL;
          if (typeid(animal) == typeid(Animal)){
              cout &lt;&lt; "Found animal" &lt;&lt; endl;
          }else{
              cout &lt;&lt; "Found animal" &lt;&lt; endl;
              cout &lt;&lt; animal-&gt;getName() &lt;&lt; " is " &lt;&lt; animal-&gt;getAge() &lt;&lt; endl;
          }
  
  
  
      }
  
  
      return 0;
  };
  </code></pre>
  
  <p><strong>output</strong></p>
  
  <pre><code>Found Dog
  Ellie is 1
  Found Dog
  bob is 6
  Found Dog
  rex is 5
  Found Dog
  shilo is 4
  Found Dog
  lindsay is 3
  Found Dog
  sam is 2
  Found Dog
  nick is 11
  </code></pre>
  
  <p>But they all should be multiplied by 7. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I added the virtual type to the methods in the Animal class above and added an Object of type Animal to my vector. </p>
  
  <p>However, in the for loop in the main, the object returns as type dog as well. </p>