1 / 18

Enhancing Code Efficiency with Templates in C++

Learn how templates in C++ allow for generic programming, reducing the need for repetitive code by using parameterized types.

anitap
Download Presentation

Enhancing Code Efficiency with Templates in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Templates INFSY 440 Spring 2004 Lecture 8

  2. 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!

  3. 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.

  4. Templates • Function Overloading: • void swap_values (int& var1, int& var2); • void swap_values (char& var1, char& var2); • void swap_values (double& var1, double& var2);

  5. Templates • void swap_values (int& var1, int& var2) • { • int temp; • temp = var1; • var1 = var2; • var2 = temp; • }

  6. Templates • void swap_values (char& var1, char& var2) • { • char temp; • temp = var1; • var1 = var2; • var2 = temp; • }

  7. Templates • void swap_values (double& var1, double& var2) • { • double temp; • temp = var1; • var1 = var2; • var2 = temp; • }

  8. 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.

  9. Function Template • template<class T> • void swap_values (T& var1, T& var2) • { • T temp; • temp = var1; • var1 = var2; • var2 = temp; • }

  10. Function Template • Function Call: • intvar1 = 9, var2 = 4; • swap_values(var1, var2); • char symbol1 = ‘a’, symbol2 = ‘z’; • swap_values(symbol1, symbol2);

  11. 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.

  12. 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

  13. 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.

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

  15. 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) • …….. • }

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

  17. 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.

  18. Let’s Practice • Where: • Area = width * length

More Related