Templates Templates

Reviews
Shared by: alllona
Categories
Stats
views:
509
rating:
not rated
reviews:
0
posted:
11/15/2008
language:
SLOVAK
pages:
0
CS1C – Advanced Programming in C++ Templates Chapter 13 Saddleback College Spring 2008 – J. Tateyama CS1C – Saddleback College Templates One of the most important features of OOP involves software reusability and generic programming which can be accomplished in C++ by using templates Two types – template functions – used to write generic functions that can be used with an arbitrary data type – template classes – used to write generic classes that can generate classes of different types For example we might write a single template to define a function that sorts arrays. Given this general description of the process, C++ could generate functions to sort arrays of different types (int, float, char, string etc.). Similarly, we might define a template to describe a class called Stack. The C++ compiler would generate classes for stacks of different types. CS1C – Saddleback College Function Templates All function template definitions begin with the keyword template followed by a comma-separated list of formal parameters enclosed in angle brackets (< >) The template parameter list cannot be empty Each item in the list is preceded by the keyword class, or the keyword typename which can be used interchangable Standard Template Library (STL) generic algorithms are implemented as function templates Review Example SW 13-3 & 13-4 Example SW 13-5 shows version using function prototype template T MinVal(T n1, T n2, T n3); CS1C – Saddleback College Template Function Array Example template void outputArray( { for(int i=0,i //pass array and size CS1C – Saddleback College Template Function Summary Some compilers do not allow separate compilation of templates. You might need to include your template definition with the code that uses it. The next standard will provide a means to externalize templates. Templates provide a way for the definition of a function or class to have parameters used for type names. Given the template prefix template T can be any C++ defined or user-defined data type (struct, class, int, float etc.). A function template may be declared inline just as a nontemplate function. The specifier is placed following the template parameter list and before the return type as shown: template inline T MinVal(T n1, T n2, T n3); OR all on one line as: template inline T MinVal(T n1, T n2, T n3); If the function is designed to work with class objects and operators (+,,*,=,etc) are used in the function, the class must contain code for the overloading of there operators CS1C – Saddleback College Function Template Extra Credit C++ provides three versions of the absolute value function – abs – labs – fabs argument is an int, return value is an int argument is a long, return value is a long argument is a float, return value is a float Write a function template that will create the above template functions depending on the arguments passed. Write a driver program to test the functions. Turn in source code and output from 3 runs CS1C – Saddleback College Overloading Template Functions Templates cause the compiler to produce overloaded versions of a function But a function template may also be overloaded Consider the previous template to output arrays It could be overloaded to output a specific portion of an array using: //outputs the contents of a specified portion of an //array (sub-array processing) template void outputArray(T ar[ ], int n, int m) { for(int i=n; i<=m; i++) { cout << ar[i] << endl; } cout << "\n\n"; } CS1C – Saddleback College Function Templates with Multiple Data Types Review SW 13-9 This example is a function that swaps two values Typecasting is required to swap an int and float variables CS1C – Saddleback College Dynamic Arrays In CS 1B we used arrays and stated that one problem with using an array was that the size had to be defined at compilation time For this reason, we had to select a maximum size at compile time and the potential for wasted memory was always present The use of a dynamic array avoids these types of problems. The size of a dynamic array may be entered as input from the user. Review SW 13-10 CS1C – Saddleback College Class Template Example We have been looking at the stack ADT since CS 1B We know that in this structure items are added to the front and removed from the front (LIFO) We also know that there are certain operations that may be performed on a stack (push, pop, empty, full) Use a dynamic array for the implementation – SW 1311 through 13-14 CS1C – Saddleback College Tonight’s Lab & Homework Assignments Extra Credit Homework – Function Template on Absolute Values (due 5-8) Group Project – Straight Ahead Wine Tours – 5/08 Update from group leaders as to the current status of the project. If any changes have been made to the original design explain what they are and why they were made. ANY GROUP MEMBERS WHO HAVE NOT BEEN KEEPING UP THEIR PART OF THE PROJECT WILL BE DROPPED FROM THE GROUPS ON THIS DATE. – 5-22 Softcopy due before final/no demo

Related docs
Templates
Views: 84  |  Downloads: 0
TEMPLATES
Views: 256  |  Downloads: 8
Templates
Views: 92  |  Downloads: 3
Templates
Views: 69  |  Downloads: 0
Templates
Views: 39  |  Downloads: 4
Templates
Views: 143  |  Downloads: 0
Templates
Views: 102  |  Downloads: 2
Templates
Views: 53  |  Downloads: 0
Templates
Views: 72  |  Downloads: 1
Templates
Views: 87  |  Downloads: 3
Templates
Views: 47  |  Downloads: 2
TEMPLATES
Views: 146  |  Downloads: 1
Templates
Views: 29  |  Downloads: 3
Templates
Views: 9  |  Downloads: 0
premium docs
Other docs by alllona