Download presentation
Presentation is loading. Please wait.
1
Function Templates Class Templates
C++ Templates Function Templates Class Templates
2
Templates overview Templates define families of classes and functions.
C++ templates enable generic programming. C++ supports both function and class templates. Templates may be parameterized by types, compile-time constants, and other templates. C++ templates are implemented by instantiation at compile-time.
3
Consider this situation
Suppose you wanted to define 2 functions called min that returns the minimum of 2 arguments passed into it for both integers and doubles. You can provide both functions as: int min( int a, int b) { return a < b ? a : b } double min( double a, double b) …and so on if you wanted to do this for more types
4
Using of a “function” Template
The above (plus a lot more) could also be accomplished using a template function like: template <class T> T min( T a, T b ) { return a < b ? a : b; } The symbol T is called a type parameter. It is simply a place holder that is replaced by an actual type or class when the function is invoked. Note: This function is now defined for all type where “<“ makes sense, not just the intended int and double.
5
Template header The symbol T is called a type parameter. It is simply a place holder that is replaced by an actual type or class when the function is invoked. A function template is declared in the same way as an ordinary function, except that it is preceded by the specification template <class T> The type parameter T may be used in place of ordinary types within the function definition. The word class is used to mean a class or primitive type. A template may have several type parameters, specified like this: template <class T, class U, class V>
6
Calling a templated function
Templated functions are called the same way ordinary functions are called. int m = 22; int n = 66; int min_value; min_value = min(m,n);
7
Interesting question Happens if we do this: long m = 2; // an int but with twice the bits double n = 1.8; // a float but with twice the bits. float min_value; min_value = min(m,n); Would this compile? What is min_value? Which templated function ran? int, long or float version
8
Another Interesting question
Suppose you created a class ratio which has a protected/private int num and int den field ratio r(2,3); // a ratio initialized to 2/3 float n = 0.8; int min_value = min(r,n); Would this compile? What is min_value? Why?
9
BubbleSort anything in C++
template <class T> void swap(T& x, T& y) { T temp = x; x = y; y = temp; } template<class T> void sort(T* v, int n) { for (int i = 1; i < n; i++) for (int j = 0; j=0, j < n-i; j++) if (v[j] > v[j+1]) swap(v[j], v[j+1] );
10
Class Templates Class templates work the same way as a function template except that it generates classes instead of functions. The general syntax is template<class T, …> class X { … } As with function templates, a class template may have several template parameters. Moreover, some of them can be primitive types parameters: template<class T, int n, class U> class X { … }
11
Primitive types must be constant
Of course, since templates are instantiated at compile time, values passed to primitive types must be constant: template<class T, int n> class X {}; int main() { X<float, 22> x1; // OK const int n = 44; X<char, n> x2; //OK int m = 66; X<short, m> x3 //ERROR: m must be constant } Note: Class templates are sometimes called parameterized types.
12
Member functions of Class Templates
The member functions of a class template are themselves function templates with the same template header as their class. For example: template<class T> class mathFunctions { static T square(T t) {return t*t ; } // more static math functions }; is handled in the same way that the following template function would be handled: T square(T t) {return t*t ; }
13
Compiler generated class names
It is instantiated by the compiler, replacing the template parameter T with the type passed to it. Thus, the declaration; mathFunctions<short> x; Generated the class and object class mathFunctions_short { static short square(short t) { return t*t; } } mathFunctions_short x; Note: your compiler may use some name other than mathFunctions_short for the class.
14
Example) Stack class template
template<class T> class Stack { public: Stack (int s = 100) : size(s), top(-1), {data = new T[size] ; } ~Stack() {delete [] data; } void push(const T& x) { data[++top] = x; } T pop() {return data[top--]; } int isEmpty() const { return top == -1; } int isFull() const {return top == size –1; } private: int size; int top; T* data; }
15
Use of the Stack Template class
int main() { Stack<int> intStack1(5) ; Stack<int> intStack2(10); Stack<char> charStack(8); intStack1.push(11) ; intStack2.push(22); charStack.push(‘A’); intStack2.push(intStack1.pop()); cout << intStack2.pop() << endl; if (intStack2.isEmpty() ) cout << “intStack2 is empty. \n” ; } Output: 11 22 intStack2 is empty.
16
Java (mid 90’s) change to C++ (mid 80’s)
Java calls Templates = Generics Java doesn’t use the Template header line template<class T> // C++ template declaration class Stack {…} // Templated Stack definited In Java it becomes: class Stack<T> {…} // Java generic Stack
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.