Source of timevirtual.cpp


  1: /** @file timevirtual.cpp
  2: A follow-up to timeslice.cpp.
  3: */

  5: #include <iostream>
  6: using namespace std;

  8: #include "time.h"
  9: #include "zonetime.h"


 12: void DisplayTimeWithBanner
 13: (
 14:     Time& someTime //in      And note the reference parameter this time!
 15: )
 16: /**<
 17: Display a time within a simple banner.
 18: @pre  someTime contains a Time object or an object of a derived class.
 19: @post The value in someTime has been displayed in "banner form".
 20: */
 21: {
 22:     cout << "\n***************************";
 23:     cout << "\n** The time is ";
 24:     someTime.display();
 25:     cout << "\n***************************\n";
 26: }


 29: int main()
 30: {
 31:     cout << "\nThis program illustrates the \"static binding\" problem "
 32:         "that prevents \"passing\nby reference\" from solving the "
 33:         "\"slicing problem\" we observed in timeslice.cpp.";
 34:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 36:     cout << "\nFirst we declare the same Time object as before."
 37:         "\nThen we display it with a call to DisplayTimeWithBanner().";
 38:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 39:     Time myTime(8, 30, 0);
 40:     DisplayTimeWithBanner(myTime);
 41:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 42:     cout << "\nOutput is again what you would expect, since we are again "
 43:         "\npassing a Time object and that's what the function expects.";
 44:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 46:     cout << "\nNext we declare the same ZoneTime object as before."
 47:         "\nThen we display it with a call to DisplayTimeWithBanner().";
 48:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 49:     ZoneTime yourTime(10, 45, 0, CST);
 50:     DisplayTimeWithBanner(yourTime);
 51:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 52:     cout << "\nThe hoped-for improvement does not happen, and once again "
 53:         "we do not get\nthe time zone part of the ZoneTime object printed "
 54:         "out. The reason is that\neven though no \"slicing\" takes place "
 55:         "this time (because of the passing by\nreference) the compiler "
 56:         "still has to decide ***at compile-time*** which\ndisplay() function "
 57:         "is going to be used, the one on the Base class or the\none in the "
 58:         "derived class. Because the parameter being passed is of type\nTime "
 59:         "and \"static binding\" is used, the display() from the Time class "
 60:         "is\nthe one chosen to be called.";
 61:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 63:     cout << "\nQuestion: So ... what to do?";
 64:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 66:     cout << "\nAnswer:"
 67:         "\nGo back to the Time class specification file and make the "
 68:         "display() function\na \"virtual function\" by simply placing "
 69:         "the keyword \"virtual\" in front of its\nprototype. This tells "
 70:         "the compiler to insert the necessary code to allow the\ndecision "
 71:         "to be made at run-time as to which version of the display() "
 72:         "function\nwill be called. This solves our problem.";
 73:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 74: }