Demonstrate when a function is overloaded at compile time vs. overriden at runtime - C++ Function

C++ examples for Function:Function Overload

Description

Demonstrate when a function is overloaded at compile time vs. overriden at runtime

Demo Code

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

class Employee//from  www . j  a v a 2s.  c  o m
{
  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;
}

Result


Related Tutorials