Sunday, January 8, 2012

Polymorphism without Inheritance

The num_sequence class of Section 4.10 simulates polymorphism. Each class object can be made
into any of the six numerical sequences at any point in the program through the set_sequence()
member function:

The ability to change the sequence type of ns is supported through programming rather than
through direct support of the language. Each class object contains an _isa data member that
identifies the current numeric sequence that it represents:




Running Codes
for (int ix = 1; ix < num_sequence::num_of_sequences(); ++ix)
{
ns.set_sequence(num_sequence::nstype(ix));
int elem_val = ns.elem(pos);
// ...
}


class num_sequence {
public:
// ...
private:
vector *_elem; // addresses current element vector
PtrType _pmf; // addresses current element generator
ns_type _isa; // identifies current sequence type
// ...
};

No comments:

Post a Comment