[an error occurred while processing this directive]
CHAPTER 6 TEMPLATE CLASSES
How do I...
- 6.1 Create a template class to represent any simple data type and understand how to use the template in a working C++ program?
- 6.2 Create a template class to represent any simple data type and extend it to read in data to a variable of any data type?
- 6.3 Create a template class to represent a compound data type and understand how to use the template in a working C++ program?
- 6.4 Write a template class that has two undefined data types that can be resolved at a later time?
- 6.5 Use a template class to handle a structure?
C++ is a strongly typed language. This means the compiler checks to see whether an integer variable is being assigned an integer value, a float variable is being assigned a float value, and so on. This is good, safe programming practice because it imposes discipline on the programmer. However, this same imposed discipline also imposes the need to write unique solutions for every data type even if the problem is the same.
This problem was realized way back in the early days of C++; therefore, a concept known as template classes was incorporated into the language. A template class is a mechanism that allows a single solution to a problem to be written that can satisfy all data types. The actual data type required can be specified later and the template class can then be used for a wide range of data types, all using the same C++ template.
6.1 Create a Template Class to Represent Any Simple Data Type and Understand How to Use the Template in a Working C++ Program
The processes performed on simple data are often fairly standard. However, using conventional programming, those processes would have to be written for every conceivable data type likely to be encountered. This means any class that operates in such a way potentially requires many definitions. Writing and maintaining this code can be a lengthy and costly business. Using a template class, you can overcome this problem and write a single solution that meets all your needs for the desired process.
6.2 Create a Template Class to Represent Any Simple Data Type and Extend It to Read in Data to a Variable of Any Data Type
The process of using a data constant embedded within a program presents no real problem. However, rather than storing and displaying a data constant, data from the keyboard often needs to be read in. A template class that can be customized to handle any simple C++ data type is initially set up and then customized in the main program to handle a specific simple data type.
6.3 Create a Template Class to Represent a Compound Data Type and Understand How to Use the Template in a Working C++ Program
Much of computing is about the storage of large quantities of data. A data structure used to store this data is called an array and is referred to as a compound data type. As with simple data types, many class definitions are needed to store every conceivable compound data type, and again the problem is overcome with the use of templates.
6.4 Write a Template Class That Has Two Undefined Data Types That Can Be Resolved at a Later Time
In practice, most, if not all, computer programs deal with multiple variables of varying data types. If several data types are involved and similar processes are applied to them, many definitions of those processes would be required. The definition of a process to deal with several variables can also be captured in a template.
6.5 Use a Template Class to Handle a Structure
The problem with structures is that they are a user-defined data type, and therefore have an infinite number of possible definitions. The problem appears to be that a template cannot be written for a problem that has an infinite number of solutions. However, this is not the case, and structures can actually be dealt with by the same solution as a simple variable. Thus, templates can be used to solve this problem.
6.1 Create a template class to represent any simple data type and understand how to use the template in a working C++ program?
Problem
Often when designing a program, it is required to perform a specific operation on different simple data types. This means any class that has such a requirement potentially requires many definitions of that class to deal with all conceivable data types likely to be encountered. This can be a lengthy and costly process.
Technique
By using a template, you can design a single class that operates on data of many types instead of creating a separate class for each individual type you are likely to use. This means you can reduce this duplication to a single class definition. In turn, this significantly reduces source code size and increases code flexibility without compromising type safety.
Templates are used to create a class that can operate on data of any type. The advantages are that templates are easy to write, and you create only one generic version of your class instead of writing many specialized but similar variations of the same thing. In addition, template classes are said to be type-safe because the types the template acts upon are known at compile time. At compile time, type checking is performed and errors are picked up before they occur at runtime.
|