Understanding C++ Templates for Flexible Programming

cs 132 winter 2024 n.w
1 / 7
Embed
Share

Explore the concept of C++ templates through lectures and examples, including template functions, classes, and the implementation details. Learn how templates allow for writing flexible code that can work with various data types efficiently.

  • C++
  • Templates
  • Programming
  • Functions
  • Classes

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CS 132, Winter 2024 Lecture 41: templates; friends Thank you to Marty Stepp and Stuart Reges for parts of these slides

  2. Template function template<typename T> returntypename(parameters) { statements; } Template: A function or class that accepts a type parameter(s). Allows you to write a function that can accept many types of data. Avoids redundancy when writing the same common operation on different types of data. Templates can appear on a single function, or on an entire class. Java has a similar mechanism called generics. 2

  3. Template func example template<typename T> T max(T a, T b) { if (a < b) { return b; } else { return a; } } The template is instantiated each time you use it with a new type. The compiler actually generates a new version of the code each time. The type you use must have an operator < to work in the above code. int i = max(17, 4); // T = int double d = max(3.1, 4.6); // T = double string s = max(string("hi"), // T = string string("bye")); 3

  4. Template class Template class: A class that accepts a type parameter(s). In the header and cpp files, mark each class/function as templated. Replace occurrences of the previous type int with T in the code. // ClassName.h template<typename T> class ClassName { ... }; // ClassName.cpp template<typename T> typeClassName::name(parameters) { ... } 4

  5. Template .h and .cpp Because of an odd quirk with C++ templates, the separation between .h header and .cpp implementation must be reduced. Either write all the bodies in the .h file (suggested), Or #include the .cpp at the end of .h file to join them together. // ClassName.h #ifndef _classname_h #define _classname_h template<typename T> class ClassName { ... }; #include "ClassName.cpp" #endif // _classname_h 5

  6. Exercise: Vector Template Convert our Vector.h and Vector.cpp into a template version of Vector.h that can store any type of data. Client code such as the following should work: Vector<int> list1; list1.add(42); list1.add(17); Vector<string> list2; list2.add("hello"); list2.add("goodbye"); 6

  7. Exercise: SortedVector Write a new class called SortedVector that works the same way as our Vector class except that it always keeps the vector in sorted order. Do you need to add or remove any member functions? Do you need to alter member functions? Do all your SortedVector operations have the same runtime as your Vector operations? 7

More Related Content