fork download
  1. //
  2. // OverloadOverride - demonstrate when a function is
  3. // declare-time overloaded vs. runtime
  4. // overridden
  5. //
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. class Student
  12. {
  13. public:
  14. // uncomment one or the other of the next
  15. // two lines; one binds calcTuition() early and
  16. // the other late
  17. // float calcTuition()
  18. virtual float calcTuition()
  19. {
  20. cout << "We're in Student::calcTuition" << endl;
  21. return 0;
  22. }
  23. };
  24.  
  25. class GraduateStudent : public Student
  26. {
  27. public:
  28. float calcTuition()
  29. {
  30. cout << "We're in GraduateStudent::calcTuition"
  31. << endl;
  32. return 0;
  33. }
  34. };
  35.  
  36. void fn(Student& x)
  37. {
  38. x.calcTuition(); // to which calcTuition() does
  39. // this refer?
  40. }
  41.  
  42. int main(int nNumberofArgs, char* pszArgs[])
  43. {
  44. // pass a base class object to function
  45. // (to match the declaration)
  46. Student s;
  47. fn(s);
  48.  
  49. // pass a specialization of the base class instead
  50. GraduateStudent gs;
  51. fn(gs);
  52. return 0;
  53. }
  54.  
  55.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
We're in Student::calcTuition
We're in GraduateStudent::calcTuition