Templates, Static Members - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Templates and Static Members, Instantiation of a class template, File scope, Templates Conclusion, Reliability of a program, Generic Algorithms Revisited, Dereferencing operator are main points of this lecture.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 39
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Templates, Static Members - Object Oriented Programming - Lecture Slides 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

…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

…Templates & Static Members

  • Output

ia.data = 5 ca.data = 7

…Templates – Conclusion

  • Templates affect reliability of a program

template< typename T > bool isEqual( T x, T y ) { return ( x == y ); }

…Templates – Conclusion

  • One may use it erroneously

int main() { char* str1 = “Hello ”; char* str2 = “World!”; isEqual( str1, str2 ); // Compiler accepts! }

…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;

}

…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 (!=)

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 getIndex() const; void setIndex( int i ); T& operator *(); bool operator !=( const Vector< T >& v ); Vector< T >& operator ++();

};

…Example – Vector

template< class T >

Vector& Vector::operator ++() {

if ( index < size ) ++index; return *this;

}

template< class T >

T& Vector< T >::operator *() {

return ptr[index];

}

…Example – Vector

template< class T >

bool Vector::operator !=( Vector& v ) { if ( size != v.size || index != v.index ) return true;

…Example – Vector

int main() {

Vector iv( 3 ); iv[0] = 10; iv[1] = 20; iv[2] = 30; Vector beyond( iv ),found( 3 ); beyond.setIndex( iv.getSize() ); found = find( iv, beyond, 20 ); cout<<“Index: ”<

Generic Algorithm

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

return start; }