Free PSE Pro Database Download
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Steps

1.  Before diving in, I will introduce you to the “data type” that the entire process hinges on. In C++, the letter T is used to specify an unknown data type. The use of the letter T is a convention so other letters will be recognized by the compiler. You will see the use of other letters later in this chapter. T is used whenever I want to specify a data type that will be defined later. This will be used when the template is applied to a specific application of the class after the actual data type is known.
2.  In the first example given in Listing 6.1, you will learn how to set up a template class that contains a single variable whose data type will be defined in the main program.
3.  The first step is to define the class. Figure 6.1 shows a very simple example that holds a single member variable and a single member function to display the contents of that member variable.


Figure 6.1  A simple template class definition.

4.  Defining the constructor is just like defining a normal member function except that you prefix the definition with the line template <class T> and specify the input data type as T (see Figure 6.2).
5.  The member function is even easier. Just prefix the definition with the instruction template <class T> as shown in Figure 6.3.


Figure 6.2  A simple template class constructor definition.


Figure 6.3  A simple template member function definition.

6.  So far, you have set up the class definitions and created a template class that can be of any data type. When you move into the main program you tell the class what the actual data type is to be. In this example, choose the integer data type (see Figure 6.4).


Figure 6.4  Customizing the template class to integer.

7.  The action is virtually identical when the template class is converted into a character class (see Figure 6.5).


Figure 6.5  Customizing the template class to character.

8.  When the template class is defined to double the process is identical (see Figure 6.6).


Figure 6.6  Customizing the template class to double.

How It Works

Listing 6.1 shows how the class is defined to accept any simple data type. The constructor and single member function are also type independent. In the main body of the program, three instances of the generic template class are created. The first object, x, has its member variable data created as type integer. The second object, y, is created using type char and the third object, z, is created using type double. The program simply shows how the single template class can be configured to deal with any simple data type.

Listing 6.1 A Single Class Template with a Simple Member Variable; Three Objects Are Created, Each Using a Different Data Type

// Template using simple data types.
// T is used to specify unknown data type.

#include <iostream.h>

template <class T>
class TSimple {
	    private:
		   T data;
	       public:
		   TSimple(T n);
		      void Show();
	    };

template <class T>
TSimple<T>::TSimple(T n)
{
    data = n;
}

template <class T>
void TSimple<T>::Show()
{
    cout << data << endl;
}

main()
{
    TSimple<int> x(25);
       x.Show();
    TSimple<char> y(‘P’);
       y.Show();
    TSimple<double> z(1.25);
       z.Show();
    return(0);
}

The screen output should look something like this:

25
P
1.25

Comments

Try changing the code to use other simple data types such as float and see how flexible the template class can be.

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?

Problem

The problem here is very similar to that described in the previous How-To. This time however, rather than storing and displaying static data, you are reading in some data from the keyboard. You do not know what that data type is initially, but you can set up a template class that can be customized to handle any simple C++ data type.

Technique

The technique is virtually identical to the technique introduced in the previous How-To. All you need to do is add a member function to your template class that asks the user to enter some data. This new member function is set up to handle any simple data type. Within the main program when you create an object based upon the class, instruct the class to handle a simple data type such as an integer or character. After that has been done, all data types represented by T become an integer or character.

Steps

1.  All you need to do is add a new member function, which I have called Gather(). Notice that I have changed the signature of the constructor. In the previous How-To, the constructor was used to initialize a data constant in your object. In this example, you are using Gather() to collect data at runtime.
template <class T>
class TSimple    {
		private:
		       T data;
		public:
		       TSimple();
		    void Gather();
		    void Show();
		};
2.  Next, you must give a definition of your new Gather() member function. Other than the first line describing the template, it is exactly the same as a normal OOP definition of a member function.
template <class T>
void TSimple<T>::Gather()
{
    cout << “Enter the data : “;
    cin >> data;
}
3.  In the main body of the program, create an instance of TSimple and stamp the data type as integer. From then on, simply invoke the member function in normal OOP fashion and it behaves in an integer manner.
main()
{
    TSimple<int> x;
    x.Gather();
    x.Show();
    return(0);
}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.