Templates INFSY 440 Spring 2004 Lecture 8 Templates

  • Slides: 18
Download presentation
Templates INFSY 440 Spring 2004 Lecture 8

Templates INFSY 440 Spring 2004 Lecture 8

Templates Functions are written to match a specific algorithm. – What if they could

Templates Functions are written to match a specific algorithm. – What if they could be written to be more generic? – Could reduce amount of coding needed!

Templates Mechanism for generating functions and classes based on generic data type. – templates

Templates Mechanism for generating functions and classes based on generic data type. – templates are sometimes called "parameterized types" By using templates, you can design a single function or class that operates on data of many types, instead of having to create a separate function or class for each type.

Templates Function Overloading: void swap_values (int& var 1, int& var 2); void swap_values (char&

Templates Function Overloading: void swap_values (int& var 1, int& var 2); void swap_values (char& var 1, char& var 2); void swap_values (double& var 1, double& var 2);

Templates void swap_values (int& var 1, int& var 2) { int temp; temp =

Templates void swap_values (int& var 1, int& var 2) { int temp; temp = var 1; var 1 = var 2; var 2 = temp; }

Templates void swap_values (char& var 1, char& var 2) { char temp; temp =

Templates void swap_values (char& var 1, char& var 2) { char temp; temp = var 1; var 1 = var 2; var 2 = temp; }

Templates void swap_values (double& var 1, double& var 2) { double temp; temp =

Templates void swap_values (double& var 1, double& var 2) { double temp; temp = var 1; var 1 = var 2; var 2 = temp; }

Function Template prefix: – tells the compiler that the definition or prototype that follows

Function Template prefix: – tells the compiler that the definition or prototype that follows is a template, – and the variable is a type parameter template<class T> NOTE: word class actually means type.

Function Template template<class T> void swap_values (T& var 1, T& var 2) { T

Function Template template<class T> void swap_values (T& var 1, T& var 2) { T temp; temp = var 1; var 1 = var 2; var 2 = temp; }

Function Template Function Call: int var 1 = 9, var 2 = 4; swap_values(var

Function Template Function Call: int var 1 = 9, var 2 = 4; swap_values(var 1, var 2); char symbol 1 = ‘a’, symbol 2 = ‘z’; swap_values(symbol 1, symbol 2);

How to Define Templates 1 Write a version of a function normally. 2 Completely

How to Define Templates 1 Write a version of a function normally. 2 Completely debug the ordinary function. 3 Then convert the ordinary function to a template by replacing some data type names with a type parameter.

Let’s Practice! Create a project with one file – main. cpp Write a function

Let’s Practice! Create a project with one file – main. cpp Write a function to square an int number Write main. cpp to prompt a user to enter an int number and call the function to generate it’s square Change it to a template function and try is with an int and a double number

Algorithm Abstraction Express an algorithm in a very general way so that we can

Algorithm Abstraction Express an algorithm in a very general way so that we can ignore incidental detail and concentrate on the substantive part of the algorithm. Function templates are one feature of C++ that supports algorithm abstraction.

Class Templates Start with template prefix template<class T> class Pair { public: ……. –

Class Templates Start with template prefix template<class T> class Pair { public: ……. – “T” is the traditional type parameter

Class Templates Member Function template<class T> void Pair<T>: : set_element(int position, T value) {

Class Templates Member Function template<class T> void Pair<T>: : set_element(int position, T value) { if (position == 1) first = value; else if (position == 2) ……. . }

Class Templates Declare objects of the class int main( ) { Pair<int> score; Pair<char>

Class Templates Declare objects of the class int main( ) { Pair<int> score; Pair<char> seats; ……. score. set_element(1, 2);

Let’s Practice Write a program to compute the area of a rectangle using the

Let’s Practice Write a program to compute the area of a rectangle using the member variables and functions shown in the UML diagram on the next slide. When the program works, change it to a template class so it will accept any type of numeric input.

Let’s Practice Where: – Area = width * length

Let’s Practice Where: – Area = width * length