Presentation is loading. Please wait.

Presentation is loading. Please wait.

Moshe Fresko Bar-Ilan University Object Oriented Programming

Similar presentations


Presentation on theme: "Moshe Fresko Bar-Ilan University Object Oriented Programming"— Presentation transcript:

1 Moshe Fresko Bar-Ilan University Object Oriented Programming 2006-2007
Templates in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming

2 Templates Templates A simple way to represent a wide range of general concepts. Direct support for generic programming. C++ Templates allow a type to be a parameter in the definition of a class or function Standard Template Library Every STL abstraction is represented as a template (string, ostream, list, map) Also key operations like sort(v), output operator <<

3 Example: Simple String Template
A class that holds characters To Provide operations like concatenation, substring, etc. Different kinds of Strings With signed char, or unsigned char With Hebrew chars etc. Expectation The char type can be copied

4 Example: Simple String Template
template<class C> class String { struct SRep ; SRep* rep ; public: String() ; String( const C* ) ; String( const String& ) ; int length() const ; char get( int index ) const ; } ; // Usage in the program String<char> cs ; String<unsigned char> ucs ; String<wchar_t> wcs ; String<HebrewChar> hebstr ;

5 Example: Simple String Template
After suitable definitions String can be used as in: int main() { String<char> buf ; map<String<char>,int> m ; while (cin>>buf) m[buf]++ ; } String <HebrewChar> buf ; map<String<HebrewChar>, int> m ;

6 string class in STL STL provides basic_string as a template string type. template<class _E, class _Tr = char_traits<_E>, class _A = allocator<_E> > class basic_string { // ... } It defines string with the regular char. typedef basic_string<char> string ; Example usage: int main() { string buf ; map<string,int> m ; while (cin>>buf) m[buf]++ ;

7 Defining a Template Template member definitions are done in the same parameterized format template<class C> struct String<C>::SRep { C* s ; // Pointer to elements int sz ; // Number of elements int n ; // Reference count // … } ; C String<C>::get( int index ) const { return rep->s[i] ; } String<C>::String() { rep = new SRep(0,C()) ; }

8 Template Parameters A template can take Example Type parameters
Ordinary parameters such as int Template parameters Example template<class T, T default_value> class Cont { /* … */ } ; // Cont<int,5> a ; // Cont<Complex,Complex(3,5)> b ; template<class T, int max_size> class Buffer { T v[max_size] ; int sz ; public: Buffer() : sz(max_size) { } // … } ; // Buffer<char,127> cbuf ; // Buffer<Record,8> rbuf ;

9 Function Templates Declaration Usage Definiton
template<class T> void sort(vector<T>&) ; Usage void f(vector<int>& vi, vector<string>& vs) { sort(vi) ; // sort<int>(vi) ; sort(vs) ; // sort<string>(vs) ; } Definiton template<class T> void sort(vector<T>& v) { for (int gap=v.size()/2;0<gap;gap/=2) for (int i=gap;i<n;++i) for (int j=i-gap;0<=j;j-=gap) if (v[j+gap]<v[j]) swap(v[j],v[j+gap]) ; operator < is used for comparison, so for any T it must have been defined.

10 Template Arguments to Specify Strategy
template<class T, class C> int compare ( const String<T>& str1, const String<T>& str2 ) { for ( int i=0;i<str1.length() && i<str2.length();++i) if(!C::eq(str1[i],str2[i])) return C::lt(str1[i],str2[i]) ? -1 : +1 ; return str1.length()-str2.length() ; } template<class T> class Cmp { // Default public: static int eq(T a,T b) { return a==b ; } static int lt(T a, T b) { return a<b ; } } ; // We can define new comparisons like: // different alphabets or case-insensitive etc. // Default definition can be provided: // template<class T, class C=Cmp<T> >

11 Exercises Write Stack class with a single template class.
Complete the String class. Define a sort function that takes a comparison criteria. Define a class Point including real valued x and y coordinates. Run the sort algorithm for sorting according to x first and then according to y first. Write a template sum function that gets as an argument a list of elements.


Download ppt "Moshe Fresko Bar-Ilan University Object Oriented Programming"

Similar presentations


Ads by Google