//
// OverloadOverride - demonstrate when a function is
// declare-time overloaded vs. runtime
// overridden
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
class Student
{
public:
// uncomment one or the other of the next
// two lines; one binds calcTuition() early and
// the other late
// float calcTuition()
virtual float calcTuition()
{
cout << "We're in Student::calcTuition" << endl;
return 0;
}
};
class GraduateStudent : public Student
{
public:
float calcTuition()
{
cout << "We're in GraduateStudent::calcTuition"
<< endl;
return 0;
}
};
void fn(Student& x)
{
x.calcTuition(); // to which calcTuition() does
// this refer?
}
int main(int nNumberofArgs, char* pszArgs[])
{
// pass a base class object to function
// (to match the declaration)
Student s;
fn(s);
// pass a specialization of the base class instead
GraduateStudent gs;
fn(gs);
return 0;
}
Ly8KLy8gIE92ZXJsb2FkT3ZlcnJpZGUgLSBkZW1vbnN0cmF0ZSB3aGVuIGEgZnVuY3Rpb24gaXMKLy8gICAgICAgICAgICAgICAgICAgICBkZWNsYXJlLXRpbWUgb3ZlcmxvYWRlZCB2cy4gcnVudGltZQovLyAgICAgICAgICAgICAgICAgICAgIG92ZXJyaWRkZW4KLy8KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIFN0dWRlbnQKewogIHB1YmxpYzoKICAgIC8vIHVuY29tbWVudCBvbmUgb3IgdGhlIG90aGVyIG9mIHRoZSBuZXh0CiAgICAvLyB0d28gbGluZXM7IG9uZSBiaW5kcyBjYWxjVHVpdGlvbigpIGVhcmx5IGFuZAogICAgLy8gdGhlIG90aGVyIGxhdGUKLy8gICAgICAgICAgZmxvYXQgY2FsY1R1aXRpb24oKQogICAgdmlydHVhbCBmbG9hdCBjYWxjVHVpdGlvbigpCiAgICB7CiAgICAgICAgY291dCA8PCAiV2UncmUgaW4gU3R1ZGVudDo6Y2FsY1R1aXRpb24iIDw8IGVuZGw7CiAgICAgICAgcmV0dX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