A virtual function is found in the GenArray class. We declare the function
print as
virtual void print() = 0;This has the effect of saying to the compiler: the implementation of print will not be given ffor the GenArray class (which makes sense - note that there aren't any array members in the class). When there is pure virtual function in a class, then we may not declare any objects of that class. In particular, we may not create any object of type GenArray. The only thing we can do with GenArray is use it as a base class.
when we implement GenArray, we provide a generic sort program (bubble sort), and we use the functions cmp and swapto do the comparison and exchange of elements. Note that both of these functions are declared as virtual, and that they use array indices (not array elements)as arguments. This allows us to avoid any type information in the class declaration and int eh sort definition.
The derived classes IntArray and FloatArray have an array as an appendant member; They also include implementations of the virtual functions in teh base class.
The second example, general lists, is more interesting. We use the ListCell class for deriving integer (IntCell) and string (StringCell) classes. Once we do this, we can have lists which contain a mixture of int cells and string cells.
Some technical issues: