Chapter 4

Generic Programming and Templates

Lesson Objectives

By the end of this chapter, you will be able to:

  • Understand how templates work and when to use them
  • Identify and implement templated functions
  • Implement template classes
  • Write code that works for multiple types

In this chapter, you will learn how to use templates effectively in your program.

Introduction

When programming, it is common to face problems that are recurring for different types of objects, such as storing a list of objects, or searching elements in a list, or finding the maximum between two elements.

Let's say that in our program we want to be able to find the maximum between two elements, either integers or doubles. With the features we have learned so far, we ...

Get C++ Fundamentals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.