Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object-Oriented Programming: Templates, Static Members, and Generic Algorithms, Slides of Object Oriented Programming

The concepts of templates and static members in object-oriented programming (oop) through a lecture series on docsity.com. The use of templates in classes a and the impact of static members on reliability. Additionally, it discusses the implementation of a generic algorithm for finding an element in a container and its application to the vector class.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(6)

116 documents

1 / 11

Toggle sidebar

Related documents


Partial preview of the text

Download Object-Oriented Programming: Templates, Static Members, and Generic Algorithms and more Slides Object Oriented Programming in PDF only on Docsity! Object-Oriented Programming (OOP) Lecture No. 39 Templates & Static Members ►Each instantiation of a class template has its own copy of static members ►These are usually initialized at file scope docsity.com …Templates & Static Members template< class T > class A { public: static int data; static void doSomething( T & ); }; …Templates & Static Members int main() { A< int > ia; A< char > ca; ia.data = 5; ca.data = 7; cout << “ia.data = ” << ia.data << endl << “ca.data = ” << ca.data; return 0; } docsity.com Generic Algorithms Revisited template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; return start; } …Generic Algorithms Revisited int main() { int iArray[5]; iArray[0] = 15; iArray[1] = 7; iArray[2] = 987; … int* found; found = find(iArray, iArray + 5, 7); return 0; } docsity.com …Generic Algorithms Revisited ►We claimed that this algorithm is generic ►Because it works for any aggregate object (container) that defines following three operations  Increment operator (++)  Dereferencing operator (*)  Inequality operator (!=) …Generic Algorithms Revisited ►Let us implement these operations in Vector to examine the generality of the algorithm ►Besides these operations we need a kind of pointer to track the traversal docsity.com Example – Vector template< class T > class Vector { private: T* ptr; int size; int index; // initialized with zero public: … Vector( int = 10 ); …Example – Vector Vector( const Vector< T >& ); T& operator [](int); int getIndex() const; void setIndex( int i ); T& operator *(); bool operator !=( const Vector< T >& v ); Vector< T >& operator ++(); }; docsity.com …Example – Vector int main() { Vector<int> iv( 3 ); iv[0] = 10; iv[1] = 20; iv[2] = 30; Vector<int> beyond( iv ),found( 3 ); beyond.setIndex( iv.getSize() ); found = find( iv, beyond, 20 ); cout<<“Index: ”<<found.getIndex(); return 0; } Generic Algorithm template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start; return start; } docsity.com Problems ►Our generic algorithm now works fine with container Vector ►However there are some problems with the iteration approach provided by class Vector …Problems ►No support for multiple traversals ►Inconsistent behavior  We use pointers to mark a position in a data structure of some primitive type  Here we use the whole container as marker e.g. found in the main program ►Supports only a single traversal strategy docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved