title: C++ access a derived class method using a base class pointer
body: <p>I am trying to access the <code>get_workhour()</code> function. How do I do this?
  Does polymorphism only works for overriden functions?  </p>
  
  <p>The <code>manager</code> class has a special function <code>get_workhour()</code> which is not present in base class <code>employee</code>. How can we call that function when both class's objects are stored in a vector of <code>employee</code> type, using polymorphism?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  class employee{
      protected:
          string name;
          double pay;
      public:
          employee(){
              name="xyz";
              pay =0.0;
          }
          employee(string name,double pay) {
              this-&gt;name = name;
              this-&gt;pay = pay;
          }
          string get_name(){
              return name;
          }
          virtual double get_pay(){
              return pay;
          }
      };
  
      class manager:public employee{
           protected:
               int workhour;
           public:
               manager(){
                   workhour = 0;
               }
               manager(string name,double pay,int workhour):
                   employee(name,pay){
                   this-&gt;workhour = workhour;
               }
               virtual int get_workhour(){
                   return workhour;
               }
               virtual double get_pay() {
                   return pay + 100;
               }
      };
      int main()
      {
          employee emp1("vivek",1500);
          manager m1("william",1300,10);
          vector&lt;employee*&gt; emps;
          emps.push_back(&amp;m1);
          emps.push_back(&amp;emp1);
          cout&lt;&lt;"Name of manager is : "&lt;&lt;emps[0]-&gt;get_name()&lt;&lt;endl;
          cout&lt;&lt;"Pay for the manager is :"&lt;&lt;emps[0]-&gt;get_pay()&lt;&lt;endl;
          cout&lt;&lt;"Workhour of manager is : "&lt;&lt;ptr-&gt;get_workhour()&lt;&lt;endl;
          cout&lt;&lt;"Name of employee is : "&lt;&lt;emps[1]-&gt;get_name()&lt;&lt;endl;
          cout&lt;&lt;"Pay for the employee is : "&lt;&lt;emps[1]-&gt;get_pay()&lt;&lt;endl;
          return 0;
      }
  </code></pre>