15
Chapter 3 Templates Saurav Karmakar Spring 2007

Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Embed Size (px)

Citation preview

Page 1: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Chapter 3 Templates

Saurav KarmakarSpring 2007

Page 2: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Objective

In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes

Page 3: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

3.1 What is a Template

A mechanism for writing routines that work for arbitrary types w/o knowing these types (type independent).

Most likely be seen in generic algorithm implementations, i.e. find max, swapping, sorting & searching.

Page 4: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

3.2 Function Templates typedef: keyword for defining new type

from an existing one.Ex: typedef double Object; void swap( Object &lhs, Object &rhs){

Object temp = lhs;lhs = rhs;rhs = temp; }

Page 5: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

3.2 Function Templates

A function template is not an actual function, instead it’s a design or a pattern, for what could become an actual function.

A function template is a design or pattern for a function which allows processors to generate an actual function from this design.

Page 6: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Function template example: Swap routinetypedef double Object;void swap(Object & lhs, Object & rhs){

Object tmp = lhs;lhs = rhs;rhs = tmp;

} // figure 3.1Note: swap is part of

the STL

template <class Object>

void swap(Object & lhs, Object &

rhs){

Object tmp = lhs;lhs = rhs;rhs = tmp;

}// figure 3.2

Page 7: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Swap routine used in main function:

int main(){int x =5, y = 7;double a = 2, b = 4;swap (x,y); // swap(int,int)swap(x,y); //reuse previous instantiation swap(a,b); //swap(double, double)//swap(x, b); // illegal: no matchreturn 0;

} // figure 3.3

Page 8: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

3.3 A Sorting Function Template

template <class Comparable>void insertionSort(vector<Comparable> &a){

for(int p = 1; p < a.size(); p++) {

Comparable tmp = a[p];int j;for(j = p; j > 0 && tmp < a[j-1]; j--)

a[j] = a [j –1];a[j] = tmp;

}}

Page 9: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Insertion Sort example

The given insertionSort routine work for double, int, float but not char*, (primitive string), because operator “=” and “<” are undefined.

Page 10: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

3.4 Class Templates A class can be a template.

Example: vector is a class template  C++ Class Templates are used where we have

multiple copies of code for different data types with the same logic.

If a set of functions or classes have the same functionality for different data types, they becomes good candidates for being written as Templates.

When possible, constant reference should be used instead of call by value because if object is a large object, making a copy could be inefficient. (or illegal if copy constructor is disable or not defined)

Page 11: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

A class template example:template <class Object>class MemoryCell{

public:explicit MemoryCell(const Object & initVal =

Object()):storedValue(initVal){}const Object & read() const{return storedValue;}void write(const Object & x){storedValue = x;}

private:Object storedValue;

}; // figure 3.8

Page 12: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Typical template interface

template<class object>

class ClassName{

public:

//public members

private:

//private member

};

Page 13: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Typical member implementation

template <class object>

ReturnType

ClassName<object>::memberName(parameterList) /*const*/

{

// member body

}

Page 14: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

template<class Object>class MemoryCell{

public: explicit MemoryCell(const Object & initVal = Object());

const Object & read() const;const write(const Object & x);

private:Object storedValue;

};// figure 3.10

Interface of the template

Page 15: Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates

Summary

Discussion of template facilities Template for generic algorithms