Working with Classes and InterfacesFive Fundamental Patterns

Listing 1. Snapshot of initial Name class.


class Name {

protected:

vector<string> fComponents;

public:

// get name component
virtual string component(int index) {
ASSERT((i >= 0) && (i < fComponents.size()));
return fComponents[index];
}

// set name component
virtual void component(int index, string component) {
ASSERT((i >= 0) && (i < fComponents.size()));
fComponents[index]= component;
}

// insert name component
virtual void insert(int index, string component) {
ASSERT((i >= 0) && (i < fComponents.size()));
fComponents.insert(fComponents.begin()+index, component);
}

// prepend name component
virtual void prepend(string component) {
insert(0, component);
}

// code for append, remove, etc. functions
...

// code for constructors and destructor
...
};