Adding and Overloading Member Functions

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:

  1. The derived class only adds new member functions
  2. The derived class overloads existing member functions. The derived collection class will not be used in polymorphic way.

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:

  1. Introduces the data member ivSum to hold the current sum.
  2. Adds the member function sum, which returns the current sum.
  3. Overloads the add member function so that it updates ivSum each time an integer is added to the collection.

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