/* *Code taken from http://www.cs.fiu.edu/~weiss/adspc++2/code/Iterator3.cpp */ #include #include #include #include "student.h" using namespace std; template class Iterator; template class VectorIterator; // Same as the vector, but has a getIterator method. // No extra data, no overridden methods, so non-virtual // destructor in original vector is OK! template class MyVector : public vector { public: explicit MyVector( int size = 0 ) : vector( size ) { } Iterator *getIterator( ) const { return new VectorIterator( this ); } }; // A passive iterator class protocol. Steps through its container. template class Iterator { public: virtual ~Iterator( ) { } virtual bool hasNext( ) const = 0; virtual const Object & next( ) = 0; }; // A concrete implementation of the iterator. // Could have been nested inside of MyVector! template class VectorIterator : public Iterator { public: VectorIterator( const MyVector *v ) : owner( v ), count( 0 ) { } bool hasNext( ) const { return count != owner->size( ); } const Object & next( ) { return (*owner)[ count++ ]; } private: const MyVector *owner; int count; }; int main( ) { MyVector v; v.push_back( new Student("Jane", 1121) ); v.push_back( new Student("Sally", 1122) ); v.push_back( new Student("Fred", 1123) ); v.push_back( new Student("Bill", 1124) ); v.push_back( new Student("Ronald", 1125) ); v.push_back( new Student("Renatta", 1126) ); v.push_back( new Student("Alexandra", 1127) ); cout << "Vector contents: " << endl; Iterator *itr = v.getIterator( ); while( itr->hasNext( ) ) itr->next( )->print(); delete itr; /* Complete a code fragment that will prompt the user for a search string representing a name, create an iterator, and use the iterator to determine whether a student with the user-specified name is stored in the vector. The output should indicate that the student was not found, or should indicate that the student was found and provide the position where the student was found. Example 1: The student Julie was not found. Example 2: The student Fred was found at position 3. */ return 0; }