Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.

Similar presentations


Presentation on theme: "Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type."— Presentation transcript:

1 Chapter 17 – Templates

2 Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type sum (Type a, Type b) { return (a + b); }

3 Function Templates u Called in same manner as ordinary function u Permissible to have both generic data types and ordinary data types u Treated similar to overloaded function u Need to be careful that function call data types compatible with function bodies u Useful for operations that apply to many data types Lesson 17.1

4 Overloaded Function Templates u Cannot replace overloaded functions u Perform same operations for each different data type u Can vary number of arguments u Specify more than one type of argument –Distinguished by number or distribution of types of arguments Lesson 17.1

5 Class Templates: Example Lesson 17.2 template class Class1 { private: Type value; public: Class1 ( ); void set_value (Type); Type get_value ( ); }; Name of class Data member Constructor Member functions

6 Class Template u Allows creation of object of class and use the data type of choice u Syntax to declare object –Class1 ob; –Indicates the ob.value is type double Lesson 17.2

7 Mechanics of Class Templates u Declaration for object using class template –Causes memory reserved for all data members –Causes instructions to be generated and stored for all function members u If another object with same bracketed data type declared –New memory reserved, but no new function instructions Lesson 17.2

8 Friends of Class Templates u Four cases (assuming single type parameter) –Ordinary function friend of each template class instantiated from class template –Ordinary class friend of each template class instantiated from class template –Template function – only if type parameter for function and class same –Template class – only matching type class is friend Lesson 17.2

9 Sequence Containers u Designed to directly control position of element within container u Three containers –vector –deque –list u Dynamic memory allocation used to reserve memory Lesson 17.3 Standard Template Library

10 Vectors u Need to include header vector vector1; –Declares vector1 to be vector container of int u Elements in contiguous memory locations –First element has subscript 0 u Can be accessed using array-like notation –“push” family of functions reserve memory and initialize single element u Random access Lesson 17.3

11 Deques u Need to include header deque deque1; –Declares deque1 to be deque container of char u Can be created using push_front( ) and push_back ( ) u Elements in contiguous memory locations u Can modify values with array notation –First element, subscript 0 u Random Access Lesson 17.3

12 Lists u Need to include the header list list1; –Declares list1 to be list container of doubles u Called doubly linked list –Two pointer values: one to next element and another to previous element u Not stored in contiguous memory Lesson 17.3

13 Iterators u Designed to be user-friendly pointers –Know type of container u Can go through list with ++ operator u General form for declaring container :: iterator name; u ordinary iterator needs no special header file Lesson 17.4 Standard Template Library

14 Using an Iterator u Need to initialize to point to location first then manipulate u begin ( ) member function returns object that points to memory location of first element u Can access element pointed to by iterator using unary * operator Lesson 17.4

15 Constant Iterators u General form or declaring container :: const_iterator name; Lesson 17.4 Type of container such as list, vector or deque Data type of container Valid identifier for iterator

16 List Iterators and Operators u Called bidirectional iterators u Cannot advance more than one element at a time u Use both ++ and - - to more forward and backward in list u Useable operators –unary * operator, ++, --, =, ==, and != Lesson 17.4

17 Algorithms u Different definition than dictionary u Global template functions designed to work with containers using iterators u Not member functions u called with function name and argument list name (iterator1, iterator2, iterator3); –name is name of algorithm –iterator1, iterator2, iterator3 names of iterators or return values from member functions Lesson 17.5 Standard Template Library

18 Summary u Function templates u Class templates u Three types of sequences containers are vector, deque and list u Basic components of STL are iterators, algorithms and containers u STL has both sequence and associative containers


Download ppt "Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type."

Similar presentations


Ads by Google