














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 22
This page cannot be seen from the preview
Don't miss anything!















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
ia.data = 5 ca.data = 7
template< typename T > bool isEqual( T x, T y ) { return ( x == y ); }
int main() { char* str1 = “Hello ”; char* str2 = “World!”; isEqual( str1, str2 ); // Compiler accepts! }
int main() {
int iArray[5]; iArray[0] = 15; iArray[1] = 7; iArray[2] = 987; … int* found; found = find(iArray, iArray + 5, 7); return 0;
}
template< class T >
class Vector {
private:
T* ptr; int size; int index; // initialized with zero
public:
… Vector( int = 10 );
Vector( const Vector< T >& ); T& operator ;
int getIndex() const; void setIndex( int i ); T& operator *(); bool operator !=( const Vector< T >& v ); Vector< T >& operator ++();
};
template< class T >
Vector& Vector::operator ++() {
if ( index < size ) ++index; return *this;
}
template< class T >
T& Vector< T >::operator *() {
return ptr[index];
}
template< class T >
bool Vector::operator !=( Vector& v ) { if ( size != v.size || index != v.index ) return true;
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: ”<
template< typename P, typename T > P find( P start, P beyond, const T& x ) { while ( start != beyond && *start != x ) ++start;
return start; }