Download C++ OOP: Member Templates, Specializations, Function Templates, Non-type Parameters and more Slides Object Oriented Programming in PDF only on Docsity! Object-Oriented Programming (OOP) Lecture No. 36 Docsity.com Recap – Member Templates • A class template may have member templates • They can be parameterized independent of the class template Docsity.com … Member Templates Revisited int main() { Complex< int > ic( 10, 5 ); Complex< float > fc( 10.5, 5.7 ); Complex< double > dc( 9.567898, 5 ); ComplexSet cs; cs.insert( ic ); cs.insert( fc ); cs.insert( dc ); return 0; } Docsity.com Partial Specialization • A partial specialization of a template provides more information about the type of template arguments than that of template • The number of template arguments remains the same Docsity.com Example – Partial Specialization template< class T > class Vector { }; template< class T > class Vector< T* > { }; Docsity.com Example – Complete Specialization template< class T, class U, class V > class A {}; template< > class A< int, char*, double > {}; Docsity.com Function Templates • A function template may also have partial specializations Docsity.com Example – Partial Specialization template< class T, class U, class V > void func( T, U, V ); template< class T, class V > void func( T, T*, V ); template< class T, class U, int I > void func( T, U ); template< class T > void func( int, T, 7 ); Docsity.com Partial Specialization • Following partial specialization deals with pointers to objects template< typename T > bool isEqual( T* x, T* y ) { return ( *x == *y ); } Docsity.com Using Different Specializations int main() { int i, j; char* a, b; Shape *s1 = new Line(); Shape *s2 = new Circle(); isEqual( i, j ); // Template isEqual( a, b ); // Complete Sp. isEqual( s1, s2 ); // Partial Sp. return 0; } Docsity.com Non-type Parameters • Template parameters may include non- type parameters • The non-type parameters may have default values • They are treated as constants • Common use is static memory allocation Docsity.com Example – Non-type Parameters int main() { Array< char > cArray( 10 ); Array< int > iArray( 15 ); Array< double > dArray( 20 ); return 0; } Docsity.com Example – Non-type Parameters template< class T, int SIZE > class Array { private: T ptr[SIZE]; public: Array(); … }; Docsity.com …Example – Non-type Parameters int main() { Array< char, 10 > cArray; Array< int, 15 > iArray; Array< double, 20 > dArray; return 0; } Docsity.com