Understanding Cursors and Iterators in Object-Oriented Programming, Slides of Object Oriented Programming

The concept of cursors and iterators in object-oriented programming (oop). A cursor is a pointer declared outside the container that helps traverse its elements using methods like first(), beyond(), and next(). Iterators, on the other hand, are objects that traverse containers without exposing their internal representation. They provide methods like operator * and operator ++. The document also discusses the difference between cursors and iterators and their advantages.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object
Object-
-Oriented Programming
Oriented Programming
(OOP)
(OOP)
Lecture No. 40
Lecture No. 40
Recap
Recap
Generic algorithm requires three operations
Generic algorithm requires three operations
(++, *, !=)
(++, *, !=)
Implementation of these operations in
Implementation of these operations in
Vector
Vector
class
class
Problems
Problems
No support for multiple traversals
No support for multiple traversals
Supports only a single traversal strategy
Supports only a single traversal strategy
Inconsistent behavior
Inconsistent behavior
Operator !=
Operator !=
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Cursors and Iterators in Object-Oriented Programming and more Slides Object Oriented Programming in PDF only on Docsity!

ObjectObject--Oriented ProgrammingOriented Programming

(OOP)(OOP)

Lecture No. 40 Lecture No. 40

CursorsCursors

► ► A better way is to useA better way is to use cursors cursors

► ► A cursor is a pointer that is declared outsideA cursor is a pointer that is declared outside

the container / aggregate object the container / aggregate object

► ► Aggregate object provides methods thatAggregate object provides methods that

help a cursor to traverse the elements help a cursor to traverse the elements

  (^) T* first()T* first()   (^) T* beyond()T* beyond()   (^) T* next( T* )T* next( T* )

……VectorVector

template< class T > template< class T > T* Vector< T >::next( T* current ) T* Vector< T >::next( T* current )

{ { if ( current < (ptr + size) ) if ( current < (ptr + size) ) return ( current + 1 ); return ( current + 1 ); // else // else return current; return current; } }

Generic AlgorithmGeneric Algorithm

template< typename P, typename T > template< typename P, typename T > P find( P start, P beyond, P find( P start, P beyond, const T& x ) { const T& x ) { while ( start != beyond && while ( start != beyond && start != x )start != x ) ++start; ++start;

return start; return start; } }

ExampleExample^ – –^ ProblemProblem

int main() { int main() {

Set< int > is( 3 ); Set< int > is( 3 ); is.add( 10 ); is.add( 10 ); is.add( 20 ); is.add( 20 ); is.add( 30 ); is.add( 30 ); ET* first = iv.first(); ET* first = iv.first(); ET* beyond = iv.beyond(); ET* beyond = iv.beyond(); ET* found = find(first, beyond, 20); ET* found = find(first, beyond, 20); return 0; return 0;

} } docsity.com

Works FineWorks Fine

template< typename CT, typename ET > template< typename CT, typename ET > P find( CT& cont, const ET& x ) { P find( CT& cont, const ET& x ) { ET* start = cont.first(); ET* start = cont.first(); ET* beyond = cont.beyond(); ET* beyond = cont.beyond(); while ( start != beyond && while ( start != beyond && start != x )start != x ) start = cont.next( start ); start = cont.next( start ); return start; return start; } }

IteratorsIterators

► ► Iterator is an object that traverses aIterator is an object that traverses a

container without exposing its internal container without exposing its internal

representation representation

► ► Iterators are for containers exactly likeIterators are for containers exactly like

pointers are for ordinary data structures pointers are for ordinary data structures

ExampleExample – – Generic IteratorGeneric Iterator

Iterator^ Container first() beyond() next() …

operator * operator ++ …

……Generic IteratorGeneric Iterator

template< class CT, class ET > template< class CT, class ET >

Iterator< CT, ET >::Iterator( Iterator< CT, ET >::Iterator( Iterator< CT, ET >& it ) { Iterator< CT, ET >& it ) { container = it.container; container = it.container; index = it.index; index = it.index;

} }

……Generic IteratorGeneric Iterator

template< class CT, class ET > template< class CT, class ET >

ET& Iterator< CT, ET >::operator *() ET& Iterator< CT, ET >::operator *()

{ {

return *index; return *index;

} }

IteratorsIterators – – ConclusionConclusion

► ► With iterators more than one traversal canWith iterators more than one traversal can

be pending on a single container be pending on a single container

► ► Iterators allow to change the traversalIterators allow to change the traversal

strategy without changing the aggregate strategy without changing the aggregate

object object

► ► They contribute towards data abstraction byThey contribute towards data abstraction by

emulating pointers emulating pointers docsity.com