Download presentation
Presentation is loading. Please wait.
Published byMarsha Cross Modified over 8 years ago
1
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData( 30 ); vector stringData( 10 ); vector is an example of a templatized class. The type of data stored in the vector is specified when we declare an instance of the vector. Templatization allows us to create collection classes that work for any type of data.
2
CPSC 252 Templatization Page 2 In 252 we have used a typedef to define the Item_type that is stored in a collection. typedef int Item_type; class Queue { bool enqueue(const Item_type &item); Item_type dequeue(); } Issues: Changing the type of data stored in the collection means editing the.h file. We can only have collections of one kind of data at a time. In C++ we can create templatized functions and templatized classes.
3
CPSC 252 Templatization Page 3 Function templates – swap ints Suppose we want to swap the value of two integers: void swap( int& num1, int& num2 ) { int temp = num1; num1 = num2; num2 = temp; }
4
CPSC 252 Templatization Page 4 Function templates – swap strings We can overload swap void swap( string& str1, string& str2 ) { string temp = str1; str1 = str2; str2 = temp; } This gets tedious (and error prone) Templatization allows us to define a version of the swap function that will work with any data type…
5
CPSC 252 Templatization Page 5 Function templates – templatized swap template void swap( Item_type& item1, Item_type& item2 ) { Item_type temp = item1; item1 = item2; item2 = temp; } Note: the first line of the above function definition could be replaced with: template But the typename keyword is perhaps more precise.
6
CPSC 252 Templatization Page 6 Templatized functions - calling int num1 = 10, num2 = 20; string str1 = “hello”, str2 = “world”; swap( num1, num2 ); // compiler generates a // void swap( int&, int& ); // version of function swap( str1, str2 ); // compiler generates a // void swap( string&, string& ); // version of function swap( num1, str2 ); // syntax error: data types of // parameters do not match Note: the templatized function must be compiled at the same time as the client code. swap.cpp can be #include ’ d at the head of the client code.
7
CPSC 252 Templatization Page 7 Templatized functions – calling explicitly int num1 = 10, num2 = 20; string str1 = “hello”, str2 = “world”; swap ( num1, num2 ); swap ( str1, str2 ); This points out the problem with swap ( num1, str2 ); What type should you supply? swap ( num1, str2 );
8
CPSC 252 Templatization Page 8 Class Templates The declaration of a templatized version of our Queue class may look as follows: template class Queue { bool enqueue(const Item_type &item); Item_type dequeue(); } The body of the class declaration will look no different from the version that uses a typedef to define Item_type. However, the syntax used to define each of the member functions is a little more involved…
9
CPSC 252 Templatization Page 9 The function definition for the constructor will look as follows: template Queue ::Queue() { … } and the enqueue(…) function looks like: template bool Queue ::enqueue(const Item_type& item) { … } C++ syntax at its finest! A templatized version of our Queue class will be found on the web.
10
CPSC 252 Templatization Page 10 A Style Suggestion: Since the entire templatized class needs to be compiled with the client code that uses it, and since the syntax for separate method bodies is ugly, and since we normally include.h files (and not.cpp files), then one can include the bodies of the templatized functions in the class definition. Many of the examples use the convention of a.template file, which is included in lieu of a.h file.
11
CPSC 252 Templatization Page 11 In Queue.template (or Queue.h): template class Queue { Queue(){ front = count = 0; } bool enqueue(const Item_type& item) { if (full()) return false; // Only shuffle the elements down if you must if (front + count >= CAPACITY) { for (int i = 0; i < count; i++) data[i] = data[front + i]; front = 0; } data[front + count++] = item; return true; } … }
12
CPSC 252 Templatization Page 12 Using the templatized Queue class Queue intQ; Queue stringQ; intQ.enqueue(34); stringQ.enqueue(“abc”); Attempts to mix types generate compiler errors: stringQ.enqueue(34); Queue-driver.cpp:78: error: invalid conversion from `int' to `const char*' Queue-driver.cpp:78: error: initializing argument 1 of ` std::basic_string ::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator ]'
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.