Week 4 - Week 4 1 Chapter 7: Templates Outline: Motivation...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Week 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 7: Templates Outline: Motivation Function templates Class templates 2
Background image of page 2
3 Generic functions and classes Can we write a function or implement a class to handle as many types of data as possible yet they are written once only? Yes, we can, with C++ templates C++ Templates are functions or classes that are written for one or more types not yet specified The standard template library ( STL ) is full of solutions in templates to manage collections of data.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 function min() int min ( int left, int right){ return left < right ? left : right;} This function can be used to find the min of a pair of int only . We have to write different versions of this function to find the min of a pair of floats, strings, … int min ( float left, float right){return left < right ? left : right;} int min ( string left, string right){return left < right ? left : right;}
Background image of page 4
5 Function Templates A function template is a function with data type as parameters which can be instantiated with particular types to generate a function definition Written once, and instantiated as many times as different data types are used
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A Small Function Template Example (1) $ cat getsize.cpp #include <iostream> using namespace std; // getSize() is a function template // T is a parametrized type template <typename T > int getSize( T return sizeof( T ); } 6
Background image of page 6
A Small Function Template Example (2) int main() { bool b; char c; short s; int i; long long l; float f; double d; long double g; cout << getSize(b) << endl; cout << getSize(c) << endl; cout << getSize(s) << endl; cout << getSize(i) << endl; cout << getSize(l) << endl; cout << getSize(f) << endl; cout << getSize(d) << endl; cout << getSize(g) << endl; } 7 $ g++ getsize.cpp $ a.out 1 1 2 4 8 4 8 16
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Remarks The above illustrates generic programming. Instead of coding the function getSize() 8 times, one for each of the types bool, char, short, int, long long, float, double, long double, the programmer codes one function template with a parametrized type. The compiler instantiates the needed actual function by replacing the parametrized type with the actual type. 8
Background image of page 8
Two Function Templates Example (1) $ cat functemp.cpp #include <iostream> #include <vector> // for vector<> using namespace std; // for cout, endl, vector, . .. 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Two Function Templates Example (2) class ratio { // rational number int num, den; // numerator, denominator public: ratio(int n =0 , int d =1 ) { // 2-param constructor if(d<0) { num = -n; den = -d; } // den >= 0 always else { num = n; den = d; } } bool operator < return num*rhs.den < den*rhs.num; } friend ostream & operator } ; // end of class ratio 10
Background image of page 10
Two Function Templates Example (3) template <typename T> void sortAny(vector< T int i, j; for(i=0; i<v.size()-1; i++) for(j=i+1; j<v.size(); j++) { if(v[j] < v[i]) { T t = v[j]; v[j] = v[i]; v[i] = t; } } } 11 What is the sorting algorithm?
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2011 for the course CS 1280 taught by Professor Forgotten during the Spring '09 term at National University of Singapore.

Page1 / 51

Week 4 - Week 4 1 Chapter 7: Templates Outline: Motivation...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online