



















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
This document from docsity.com covers the topic of templates in object-oriented programming (oop), specifically focusing on function templates and class templates, as well as user-defined types as arguments. It also discusses the differences between overloading and templates, and the use of templates as policies. Examples are provided to illustrate the concepts.
Typology: Slides
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















class String {
char* pStr; … // Operator “==“ not defined
};
class String {
char* pStr; … friend bool operator ==( const String&, const String& ); };
bool operator ==( const String& x,
const String& y ) { return strcmp(x.pStr, y.pStr) == 0;
}
String operator +( const char * str1, const String& y ) { String tmp; tmp.pStr = new char[ strlen(str1) + strlen(y.pStr) + 1 ]; strcpy( tmp.pStr, str1 ); strcat( tmp.pStr, y.pStr ); return tmp;
}
template< class T >
T sum( T* array, int size ) {
T sum = 0;
for (int i = 0; i < size; i++) sum = sum + array[i];
return sum;
}
int caseSencompare( char* str1,
char* str2 )
{
for (int i = 0; i < strlen( str1 ) && i < strlen( str2 ); ++i) if ( str1[i] != str2[i] ) return str1[i] - str2[i];
return strlen(str1) - strlen(str2);
}
int compare( char* str1, char* str2, bool caseSen )
{
for (int i = 0; i < strlen( str1 ) && i < strlen( str2 ); i++) if ( … ) return str1[i] - str2[i];
return strlen(str1) - strlen(str2);
} Docsity.com