Templates2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 1 Templates
2OBJECTIVESIn this chapteryou will learn:- To use function templates to convenientlycreate a group of related (overloaded)functions.To distinguish between function templates andfunction-template specializations.- To use class templates to create a group ofrelated types.To distinguish between class templates andclass-template specializationsTo overload function templates2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn: ▪ To use function templates to conveniently create a group of related (overloaded) functions. ▪ To distinguish between function templates and function-template specializations. ▪ To use class templates to create a group of related types. ▪ To distinguish between class templates and class-template specializations. ▪ To overload function templates
3ao25.1Introduction25.2Function Templates25.3Overloading Function Templates25.4Class Templates25.5Nontype Parameters and DefaultTypesforClassTemplates2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 3 25.1 Introduction 25.2 Function Templates 25.3 Overloading Function Templates 25.4 Class Templates 25.5 Nontype Parameters and Default Types for Class Templates
425.1Introduction·Generic programming-Writingcode in a way that is independent ofany particulardatatype. Templates are the foundation of generic programming.Function templates and class templates-Enableprogrammersto specifyanentirerangeofrelatedfunctionsandrelatedclasses. Called function-template specializations and class-templatespecializations, respectively Atemplateis a blueprintforcreatinga function ora class- E.g, vectoris a template class in C++ standard libraryvector<int>,vector<string> are classes generated from the vectortemplate2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 4 25.1 Introduction • Generic programming – Writing code in a way that is independent of any particular data type • Templates are the foundation of generic programming • Function templates and class templates – Enable programmers to specify an entire range of related functions and related classes • Called function-template specializations and class-template specializations, respectively – A template is a blueprint for creating a function or a class – E.g, vector is a template class in C++ standard library • vector<int>, vector<string> are classes generated from the vector template
525.1Introduction: Example of function templatetemplate <typename T>int compare(const string &vl,const string &v2)if(v1 <v2) return -1;intcompare(constT&vl,constT&v2)if (v2 < vl) return 1;if(v1 <v2) return -l;return 0;if(v2 <vl) return 1;return 0;int compare(const double &vl, const double &v2)1if(vl <v2) return -1;cout << compare(1, 0)<< endl:if (v2 < vl) return 1;return O;string sl =“hi",s2="world"cout<<compare(sl, s2)<< endl2006PearsonEducation,Inc.Allrightsreserved
© 2006 Pearson Education, Inc. All rights reserved. 5 25.1 Introduction • Example of function template int compare(const string &v1, const string &v2) { if (v1 < v2) return -1; if (v2 < v1) return 1; return 0; } int compare(const double &v1, const double &v2) { if (v1 < v2) return -1; if (v2 < v1) return 1; return 0; } template <typename T> int compare(const T &v1, const T &v2) { if (v1 < v2) return -1; if (v2 < v1) return 1; return 0; } cout << compare(1, 0) << endl; string s1 = “hi”, s2 = “world”; cout << compare(s1, s2) << endl;