5.3. 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:
for ( int ix = 1; ix < num_sequence::num_of_sequences(); ++ix ) { ns.set_sequence( num_sequence::nstype( ix )); int elem_val = ns.elem( pos ); // ... }
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:
class num_sequence { public: // ... private: vector<int> *_elem; // addresses current element vector PtrType _pmf; ...
Get Essential C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.