(Latest Revision: Fri Oct 2 18:31 PDT 2015 )
sphereClass sphere ; ballClass ball ; sphereClass * SPtr ; SPtr = &sphere ; SPtr->DisplayStatistics(); SPtr = &ball ; SPtr->DisplayStatistics();Then the sphereClass version of DisplayStatistics may be invoked both times.
sphereClass * X[26] ; // 26 pointers init (X) ; // Some are made to point to balls, others to plain spheres. int n ; cin >> n ; // how does the compiler know whether *X[n] is a ball? X[n]->DisplayStatistics() ;The virtual keyword is basically a directive that the programmer uses to mark a method (member function) in a base class. It warns the compiler that a derived class may revise the method. When the compiler gets this warning, it generates code that uses dynamic binding at runtime to aid in choosing the right version of the method to execute.
class sphereClass { public: ... // everything as before, except DisplayStatistics virtual void DisplayStatistics() const ; { // put definition of function here. } ... // all the rest of the declarations } ; // end class(Unfortunately this forces the designer of the base class to worry about the possible derived classes that may be invented.)