In this lesson, our instructor Alvin Sylvain goes through an introduction on templates. He starts by discussing function templates and constraints on types, he also explains multiple types, default types, class template, template specialization, non-type parameters, templates in templates and cautions.
Create a template function to input a “generic number”. What it will need to do is:
Read from “cin”
Add 17 to the number
Print the result to “cout”
Test it with “int”, “long”, “double”, and “float” types.
Modify it so that the input and output streams are also template parameters. That way, you can use it to read and write from any stream, include stringstream.
Modify it so that any integer, not just 17, can be added.
Write a specialized version of it for the string type, so that a string can be concatenated to the input string.
Create a template function that compares two values and returns true if they’re equal or false otherwise.
Test this with “int”, “short”, “long”, and “string”.
Create specialized versions for “float” and “double” where the values are equal if they are equal within a certain tolerance. E.g., 2.999998 is “equal” to 3.000002, because they are within a tolerance of 0.00001.
Modify it so that the tolerance value is also a parameter, with a default value of 0.00001.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.