Cursors, Vector - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Cursors, Pointer, Container, Aggregate object, Methods of Aggregate object, Traverse the elements, Vector, Generic Algorithm, Contiguous sequence, Containers, Traversal operations are points you can learn in this Object Oriented Programming lecture.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 40
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Cursors, Vector - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 40

Recap

  • Generic algorithm requires three

operations (++, *, !=)

  • Implementation of these operations in

Vector class

  • Problems
    • No support for multiple traversals
    • Supports only a single traversal strategy
    • Inconsistent behavior
    • Operator !=

Vector

template< class T > class Vector { private: T* ptr; int size; public: Vector( int = 10 ); Vector( const Vector< T >& ); ~Vector(); int getSize() const;

…Vector

const Vector< T >& operator =( const Vector< T >& ); T& operator ; T* first(); T* beyond(); T* next( T* );

};

…Vector

template< class T >

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

{

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

}

Example – Cursor

int main() {

Vector< int > iv( 3 ); iv[0] = 10; iv[1] = 20; iv[2] = 30; int* first = iv.first(); int* beyond = iv.beyond(); int* found = find(first,beyond,20); return 0;

}

…Cursors

  • This technique works fine for a contiguous

sequence such as Vector

  • However it does now work with containers

that use complicated data structures

  • There we have to rely on the container

traversal operations

Example – Works Fine

a b c d e f g …

Cursor

Example – Problem

int main() {

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

}

…Example – Problem

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

return start; }

…Works Fine

int main() {

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

}

Cursors – Conclusion

  • Now we can have more than one traversal

pending on the aggregate object

a b c d e f g …

Cursor 1 Cursor 2

Iterators

  • Iterator is an object that traverses a

container without exposing its internal

representation

  • Iterators are for containers exactly like

pointers are for ordinary data structures

Generic Iterators

  • A generic iterator works with any kind of

container

  • To do so a generic iterator requires its

container to provide three operations

  • T* first()
  • T* beyond()
  • T* next( T* )