0% found this document useful (0 votes)
66 views24 pages

Data Structures: 5. Class Templates

Class templates provide functionality to operate on different data types and facilitate code reuse. A class template is declared using the template keyword followed by a template parameter enclosed in angle brackets. For example, template<class T> declares a class template that can work with any data type. A vector class template is provided as an example that can store element of different types without needing separate vector classes for each type. Specialization of class templates is also discussed to handle types the general template may not support properly, like handling char* differently than other types for the vector class template. Member functions of a class template can also be templates to allow operations on different data types.

Uploaded by

Talal Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views24 pages

Data Structures: 5. Class Templates

Class templates provide functionality to operate on different data types and facilitate code reuse. A class template is declared using the template keyword followed by a template parameter enclosed in angle brackets. For example, template<class T> declares a class template that can work with any data type. A vector class template is provided as an example that can store element of different types without needing separate vector classes for each type. Specialization of class templates is also discussed to handle types the general template may not support properly, like handling char* differently than other types for the vector class template. Member functions of a class template can also be templates to allow operations on different data types.

Uploaded by

Talal Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures

5. Class Templates

5-Class Templates 1
Class Template

• A class template provides functionality to operate on different


types of data
– Facilitates reuse of classes

• Declaration:
template< class T >
class XYZ{ . . .};

template< typename T >


Class XYZ { . . . };

5-Class Templates 2
Class Template – Example

• A Vector class template can store data elements of different types


– Without templates, separate Vector class is needed for each data type
template< class T >
class Vector {
private:
int size;
T* ptr;
public:
Vector<T>( int = 10 );
Vector<T>( const Vector< T >& );
~Vector<T>();
int getSize() const;
const Vector< T >& operator =(const Vector< T >& );
T& operator []( int );
};

5-Class Templates 3
Class Template – Example

template< class T >


Vector<T>::Vector( int s ) {
size = s;
if ( size != 0 )
ptr = new T[size];
else
ptr = 0;
}

5-Class Templates 4
Class Template – Example

template< class T >


Vector<T>:: Vector(const Vector<T>& copy ) {
size = copy.getSize();
if (size != 0) {
ptr = new T[size];
for (int i = 0; i < size; i++)
ptr[i] = copy.ptr[i];
}
else ptr = 0;
}

5-Class Templates 5
Class Template – Example

template< class T >


Vector<T>::~Vector<T>() {
delete [] ptr;
}

template< class T >


int Vector<T>::getSize() const {
return size;
}

5-Class Templates 6
Class Template – Example
template< class T >
const Vector<T>& Vector<T>::operator =( const Vector<T>& right) {
if ( this != &right ) {
delete [] ptr;
size = right.size;
if ( size != 0 ) {
ptr = new T[size];
for(int i = 0; i < size;i++)
ptr[i] = right.ptr[i];
}
else
ptr = 0;
}
return *this;
}
5-Class Templates 7
Class Template – Example

template< class T >


T& Vector< T >::operator [](int index ) {
if ( index < 0 || index >= size ) {
cout << “Error: index out of range\n”;
exit( 1 );
}
return ptr[index];
}

5-Class Templates 8
Class Template – Usage

int main() {
Vector< int > iv1( 2 );
iv1[0] = 15;
iv1[1] = 27;
Vector< int > iv2( iv1 );
Vector< int > iv3( 2 );
iv3 = iv1;
return 0;
}

5-Class Templates 9
Class Template Specialization

int main() {
Vector< char* > sv1( 2 );
sv1[0] = "Aamir";
sv1[1] = "Nasir";
Vector< char* > sv2( sv1 );
Vector< char* > sv3( 2 );
sv3 = sv1;
return 0;
}

• Like function templates, a class template may not handle all the
types successfully
• Explicit specializations are provided to handle such types

5-Class Templates 10
Class Template Specialization
template<>
class Vector< char* > {
private:
int size;
char** ptr;
public:
// Vector< char* >( int = 10 );
Vector( int = 10 );
Vector( const Vector< char* >& );
virtual ~Vector();
int getSize() const;
const Vector< char* >& operator =(const Vector< char* >& );
const char*& operator []( int );
void insert( char*, int );
};
5-Class Templates 11
Class Template Specialization

template<>
Vector<char*>::Vector(int s) {
size = s;
if ( size != 0 ) {
ptr = new char*[size];
for (int i = 0; i < size; i++)
ptr[i] = 0;
}
else
ptr = 0;
}

5-Class Templates 12
Class Template Specialization
template<>
Vector< char* >::Vector(const Vector<char*>& copy ) {
size = copy.getSize();
if ( size == 0 ) {
ptr = 0;
return;
}
ptr = new char*[size];
for (int i = 0; i < size; i++)
if ( copy.ptr[i] != 0 ) {
ptr[i] = new char[ strlen( copy.ptr[i] ) + 1 ];
strcpy(ptr[i], copy.ptr[i]);
}
else
ptr[i] = 0;
}
5-Class Templates 13
Class Template Specialization

template<>
Vector<char*>::~Vector() {
for (int i = 0; i < size; i++)
delete [] ptr[i];
delete [] ptr;
}

template<>
int Vector<char*>::getSize() const {
return size;
}

5-Class Templates 14
Class Template Specialization
template<>
const Vector<char*>& Vector<char*>::operator=(const Vector<char*>& right) {
if ( this == &right )
return *this;
for (int i = 0; i < size; i++)
delete [] ptr[i];
delete [] ptr;
size = right.size;
if ( size == 0 ) {
ptr = 0;
return *this;
}
ptr = new char*[size];
for (int i = 0; i < size; i++)
if ( right.ptr[i] != 0 ) {
ptr[i] = new char[strlen( right.ptr[i] ) + 1];
strcpy( ptr[i], right.ptr[i] );
}
else ptr[i] = 0;
} 5-Class Templates 15
Class Template Specialization

template<>
const char*& Vector<char*>::operator []( int index ) {
if ( index < 0 || index >= size ) {
cout << "Error: index out of range\n";
exit( 1 );
}
return ptr[index];
}

5-Class Templates 16
Class Template Specialization

template<>
void Vector< char* >::insert(char* str, int i ) {
delete [] ptr[i];
if ( str != 0 ) {
ptr[i] = new char[strlen(str)+ 1];
strcpy( ptr[i], str );
}
else
ptr[i] = 0;
}

5-Class Templates 17
Class Template Specialization

int main() {
Vector< char* > sv1( 2 );
sv1[0] = “Aamir”; // Error
sv1.insert( "Aamir", 0 );
sv1.insert( "Nasir", 1 );
Vector< char* > sv2( sv1 );
Vector< char* > sv3( 2 );
sv3 = sv1;
return 0;
}

5-Class Templates 18
Member Templates

• A class or class template can have member functions that are


themselves templates.

• Example
template <typename T>
class Complex {
T real, imag;
public:
// Complex<T>( T r, T im )
Complex( T r, T im ) : real(r), imag(im) {}
// Complex<T>(const Complex<T>& c)
Complex(const Complex<T>& c): real(c.real), imag(c.imag) {}
. . .
};

5-Class Templates 19
Member Templates
template <typename T>
class Complex {
T real, imag;
public:
// Complex<T>( T r, T im )
Complex( T r, T im ) : real(r), imag(im) {}
// Complex<T>(const Complex<T>& c)
Complex(const Complex<T>& c): real(c.real), imag(c.imag) {}
. . .
};

int main() {
Complex< float > fc( 0, 0 );
Complex< double > dc = fc; // Error
return 0;
}
5-Class Templates 20
Member Templates – Reason For Error

int main() {
Complex< float > fc( 0, 0 );
Complex< double > dc = fc; // Error
return 0;
}

class Complex<double> {
double real, imag;
public:
Complex( double r, double im ) : real(r), imag(im) {}
Complex(const Complex<double>& c):real(c.real),imag(c.imag) {}
. . .
};

5-Class Templates 21
Member Templates

template <typename T>


class Complex {
T real, imag;
public:
Complex( T r, T im ) : real(r), imag(im) {}
template <typename U>
Complex(const Complex<U>& c):real( c.real ), imag( c.imag ) {}
. . .
};

5-Class Templates 22
Member Templates

int main() {
Complex< float > fc( 0, 0 );
Complex< double > dc = fc; // OK
return 0;
}

class Complex< double> {


double real, imag;
public:
Complex( double r, double im ) : real(r), imag(im) {}
template <typename U>
Complex(const Complex<U>& c):real( c.real), imag( c.imag ) {}
. . .
};

5-Class Templates 23
Any Question So Far?

5-Class Templates 24

You might also like