Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Intermediate C++
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!


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

  • Intro 0:00
  • Lesson Overview 0:08
  • Function Template 5:48
    • Define
    • Use
    • Example
  • Constraints on Types 10:34
    • Example
  • Multiple Types 13:58
    • Example
  • Default Types 17:11
    • Example
  • Class Template 20:27
    • Example
  • Template Specialization 24:31
    • Example
  • Non-Type Parameters 27:32
    • Example
  • Templates in Templates 32:20
    • Example
  • Cautions 35:04
    • Don't Use a Template with a Type That Doesn't Support All the Template's Operations
    • Templates Can Not be Split into Header Files and Compiled Units
    • Issues with Debugging
  • Templates Gone Wild! 40:33