Typically you will not derive from any of the Collection Classes. When you must derive classes from the Collection Classes be aware of the following:
Note: Collection classes do not have virtual functions. You cannot override the member functions of a collection class.
For example, suppose you want to implement a set of integers that can give you information about the sum of integers contained in the collection. You create a class IntSet that is derived from ISet<int>. This class does the following:
In a real application, any add, replace or remove member function would have to be overloaded in order to update the sum of integers. For simplicity, this is not done in the example below:
#include <iset.h> class IntSet: public ISet<int> { typedef ISet<int> Inherited; public: IntSet(INumber n = 100) : ISet<int> (n), ivSum (0) { } IBoolean add(int const& i) { ivSum += i; return Inherited::add(i); } int sum() const { return ivSum; } private: int ivSum; }; //... IntSet anIntSet; anIntSet.add(1); anIntSet.add(2); cout << anIntSet.sum () << endl;
The output of this program is 3.
Introduction
to the Collection Classes
Collection
Class Hierarchy
Overall
Implementation Structure
Collection
Class Polymorphism
Flat
Collections
Trees
Copying and Referencing
Collections
Taking Advantage of
the Abstract Class Hierarchy
Instantiating the
Collection Classes