To use a collection class, you normally follow these three steps:
The following example describes instantiation for the default implementation. Consider the following example header file for a class Person:
//person.h - Header file containing class Person #include <iostream.h> #include <istring.hpp> class Person { IString PersonName; IString TNumber; public: //constructor Person () : PersonName(""), TNumber("") {} //copy constructor Person(IString Name, IStringNumber) : PersonName(Name), TNumber(Number) { } IString const& GetPersonName() const { return PersonName; } IString const& GetTNumber() const { return TNumber; } IBoolean operator==(Person const& A) const { return (PersonName == A.GetPersonName()) && (TNumber==A.GetTNumber()); } IBoolean operator<(Person const& A) const { return (PersonName < A.GetPersonName()); } };
For a given class, such as ISet, and a given element type, such as a class named Person, the instantiation for a new class that represents sets of persons could look like this:
//main.cpp - main file #include <iset.h> #include <iostream.h> #include "person.h" //person.h from the previous example typedef ISet<Person> AddressList; void main() { AddressList Business; Person A("Peter Black", "50706"); Business.add(A); cout << "\nThe set now contains " << Business.numberOfElements() <<" entries!\n"; }
Once the AddressList collection is defined, you can define AddressList objects Family, Business, and Sportclub as follows:
AddressList Family, Business, Sportclub;
You can also define the objects without introducing a new type name (AddressList):
ISet < Person > Family, Business, Sportclub;
However, you should begin by explicitly defining a named class, such as AddressList, that uses the default implementation. It is then easier to replace the default implementation with a better implementation later on.
Introduction
to the Collection Classes
Collection
Class Hierarchy
Overall
Implementation Structure
Collection Characteristics
Ordering
of Collection Elements
Access
by Key
Equality
Relation
Uniqueness of Entries
Thread
Safety and the Collection Classes
Class Template Naming
Conventions
Possible Implementation
Paths
Choosing One of the
Provided Implementation Variants
Adding an Element to a Collection
Removing an Element from a Collection
Copying and
Referencing Collections
Replacing the Default
Implementation