《面向对象程序设计英文教学课件:12_Templates》由会员分享,可在线阅读,更多相关《面向对象程序设计英文教学课件:12_Templates(22页珍藏版)》请在金锄头文库上搜索。
1、Object-Oriented Programming & C+Object-Oriented Programming & C+College of Computer Science, CQU12 TemplatesObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplates1OutlinepFunction templatespClass templatesObject-Oriented Programming & C+Object-Oriented Programming &
2、 C+12 12 TemplatesTemplates2Knowledge PointspWriting generic functionsBook: Accelerated C+, Chapter 8Object-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplates3Reference MaterialspTemplatesBook: C+ How to Program (8th edition), Chapter 14 (15 pages)Object-Oriented Progr
3、amming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesIntroductionpMany of the ideas and examples presented in the last few lessons have illustrated ways of making code general purpose.nSuch as overloading function and inheritance pThe idea of a template is that it defines operations fo
4、r an unbounded variety of related data types.pProgramming using types as parametersObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesIntroductionpKinds of templatenFunction templatesnClass templatespThey describe, in some general way, how to manipulate data elem
5、ents. pThey work with any kind of data that supports the required operations.Object-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesTo thinkpA function max() takes two data elements as arguments, and returns the larger. We rewrite this functions for many different ty
6、pes.int max(int a, int b) if(a b)return a; elsereturn b;double max(double a, double b) if(a b)return a; elsereturn b;Box max(Box a, Box b) if(a b)return a; elsereturn b;There has to be a better way!Object-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatestemplate T max
7、(const T &a, const T &b) if (a b) return a; else return b;Indicates a template is being definedT is a formal template type parameterpFunction templatenDescribes a function format that when instantiated with particulars generates a function definitionpWrite once, use multiple timesFunction TemplatesO
8、bject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatespCode segmentint Input1 = PromptAndRead();int Input2 = PromptAndRead();cout max(Input1, Input2) b) return a; else return b;Template FunctionObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 Tem
9、platesTemplatespCode segmentdouble Value1 = 4.30;double Value2 = 19.54;cout max(Value1, Value2) b) return a; else return b;Template FunctionObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesOperator needs to be defined for the actual template parameter type. If
10、is not defined, then a compile-time error occurspCode segmentBox b1(6,4,7);Box b2(5,5,8);cout max(b1, b2) b) return a; else return b;Template FunctionObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesNo template function, just a compile-time error pThe template
11、parameters must be exact mach when instantiating a function template.pCode segmentBox b1(6,4,7);double d = 2.0;cout max(b1, d) endl;pWhat function will be generated from our template ?Template FunctionObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatestemplate T
12、Min(const T &a, const T &b) if (a b) return a; else return b;int Min(const int &a, const int &b) if (a b) return a; else return b;int main() int a = 1, b = 2; cout Min(a, b) endl; return 0;pFor following code segment, what is happened?pCompiler match overloading function first, and then match templa
13、te functionFunction TemplatesObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesAdvicebool find(int val);bool find(const Matrix &val);template bool find(elemType &val);In definition of template, the parameters of function commonly use reference.Object-Oriented Pr
14、ogramming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesClass Template declarationtemplate class class_name /definition of template;template return_type class_name : member_function_name(member_function_argument_list)/body_of_member_functionObject-Oriented Programming & C+Object-Orient
15、ed Programming & C+12 12 TemplatesTemplatestemplate class TC public: TC(); void Assign(T src); /. private: T ValueArrayn; The value template parameterThe type template parameterClass TemplatesObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesTC A;TC B;class TCpu
16、blic: TC(); void Assign(char src); /.private: char ValueArray80; ;TC A;class TCpublic: TC(); void Assign(int src); /.private: int ValueArray125; ;TC B;Class TemplatesObject-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesExample: general data storing classtemplate cl
17、ass Store/Class template: implement storing any type data private: T item; / to store data bool haveValue;/ to mark whether the data is existed public: Store(void); / default constructor T getElem(void); / to get the data void putElem(T x);/ to store the data;/ implementation of the default construc
18、tor template Store:Store(void): haveValue(0) Object-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatestemplate / implementation of the getElem()T Store:getElem(void) if (!haveValue) / if no data in store, exit the program cout No item present! endl; exit(1); return ite
19、m; / return the stored datatemplate / implementation of the putElem() void Store:putElem(T x)haveValue = true; item = x;Object-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesstruct Student int id; /学号 float gpa; /平均分;Object-Oriented Programming & C+Object-Oriented P
20、rogramming & C+12 12 TemplatesTemplatesint main() Student g= 1000, 23; Store s1, s2; Store s3; s1.putElem(3); s2.putElem(-7); cout s1.getElem() s2.getElem() endl; s3.putElem(g); cout The student id is s3.getElem().id endl;return 0;Object-Oriented Programming & C+Object-Oriented Programming & C+12 12 TemplatesTemplatesHomeworkp (Array ClassTemplate) Reimplement class ArrayList as a class template pDemonstrate the new ArrayList class template in a program.21