180 likes | 192 Views
Learn how templates in C++ allow for generic programming, reducing the need for repetitive code by using parameterized types.
E N D
Templates INFSY 440 Spring 2004 Lecture 8
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 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& var1, int& var2); • void swap_values (char& var1, char& var2); • void swap_values (double& var1, double& var2);
Templates • void swap_values (int& var1, int& var2) • { • int temp; • temp = var1; • var1 = var2; • var2 = temp; • }
Templates • void swap_values (char& var1, char& var2) • { • char temp; • temp = var1; • var1 = var2; • var2 = temp; • }
Templates • void swap_values (double& var1, double& var2) • { • double temp; • temp = var1; • var1 = var2; • var2 = temp; • }
Function Template • 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& var1, T& var2) • { • T temp; • temp = var1; • var1 = var2; • var2 = temp; • }
Function Template • Function Call: • intvar1 = 9, var2 = 4; • swap_values(var1, var2); • char symbol1 = ‘a’, symbol2 = ‘z’; • swap_values(symbol1, symbol2);
How to Define Templates • Write a version of a function normally. • Completely debug the ordinary function. • 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 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 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: ……. • “T” is the traditional type parameter
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> seats; • ……. • score.set_element(1, 2);
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