C++ Virtual function overloaded at compile time vs. overridden at runtime

Description

C++ Virtual function overloaded at compile time vs. overridden at runtime

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Employee/*from  w w w.j  a v a 2s.c om*/
{
  public:
    virtual void calcTuition()
    {
        cout << "We're in Employee::calcTuition" << endl;
    }
};

class GraduateEmployee : public Employee
{
  public:
    void calcTuition()
    {
      cout<<"We're in GraduateEmployee::calcTuition"<<endl;
    }
};

void fn(Employee& x)
{
    x.calcTuition(); // which calcTuition()?
}

int main(int nNumberofArgs, char* pszArgs[])
{
    Employee s;
    fn(s);

    GraduateEmployee gs;
    fn(gs);

    return 0;
}



PreviousNext

Related