Creating and using class and function templates is a good way to write reusable source code - as long as it's done right. In this article I will show you how to declare and implement a template class and its methods. I'll use a fairly extensive example so you can get a feel for the power designing with template classes and static polymorphism has to offer. If you've ever implemented a data structure of some kind, such as an array or binary tree, you probably tailored it to the problem at hand. If you wanted to reuse the same code later to store another type of object, you'd have to perform some extensive rewriting to get it to work.
Templates offer a solution to the problem of writing reusable code.
As an example of template usage I'll implement a binary tree that, when finished, can be used to store objects of different types. A binary tree is a data structure that has many uses in the field of computer science. A binary tree is comprised of nodes that can have zero, one or two children nodes. Refer to Figure 1. Binary Tree Figure 1 shows a binary tree with seven nodes.