Click Here!
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


How It Works

Listing 6.2 is a full program that shows how to set up the TSimple class for any simple data type. The first call deals with integers, the second call with characters, and the third call with doubles. After all the data is read in using the new Gather() member function, the data is shown to the screen.

Listing 6.2 Listing 6.2 Is an Extension of Listing 6.1 and Has an Additional Member Function That Allows Runtime Input

// Template reads in a simple data type.
// T is used to specify an unknown data type.

#include <iostream.h>

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

template <class T>
TSimple<T>::TSimple()
{
    cout << “Template class constructed” << endl;
}

template <class T>
void TSimple<T>::Gather()
{
    cout << “Enter the data : “;
    cin >> data;
}

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

main()
{
    TSimple<int> x;
    x.Gather();

    TSimple<char> y;
    y.Gather();

    TSimple<double> z;
    z.Gather();

    x.Show();
    y.Show();
    z.Show();

    return(0);
}

My screen looked like this:

Template class constructed
Enter the data : 77
Template class constructed
Enter the data : P
Template class constructed
Enter the data : 1.2345
77
P
1.2345
Press any key to continue

Try out the program and see how yours compares.

Comments

Again the simple data type can be made to be any legal C++ simple data type. I have used integer, character, and double, but there is no reason why you couldn’t use float or even bool if you wanted to. Try modifying the preceding program to accept other simple data types and observing the results.

6.3 Create a template class to represent a compound data type and understand how to use the template in a working C++ program?

Problem

Much of computing is about the storage of large quantities of data items. One storage structure that can store multiple data items is an array. As in the problem with simple data types, lots of class definitions would be needed to store every conceivable data type. The problem is overcome with the use of templates.

Technique

The technique is virtually identical to the example in the previous How-To. The main difference is you specify that the member variable holding the data of unknown data type is a pointer to a block of memory. You should recall that any array variable is in fact a pointer to an area of memory (see Figure 6.7).


Figure 6.7  The pointer to the array of as yet unknown underlying data type.

Steps

1.  The first step (as in the examples in the previous How-Tos) is to define the class. I will again give a very simple example that holds a single member variable that is a pointer to an area of memory. The size of the memory block will be defined later when the object is constructed. Member functions are used to collect the data to be stored in the array and to display the contents of the array to the screen.
template <class T>
class TArray    {
		private:
		       T *data;
		    int size;
		public:
		       TArray(int n);
		    void Gather();
		    void Show();
		};
2.  The definition of the constructor is relatively simple. A single integer input parameter specifies the size of the array. The actual underlying data type of the array is defined in the main body of the program each time you create an instance of the class. Hence, the use of the letter T.
template <class T>
TArray<T>::TArray(int n)
{
    data = new T[n];
    size = n;
}
3.  The member function that collects the data to be stored in the array is called Gather(), and other than the template directive as its first line, it is normal C++ code. It uses a for loop to read in data of an as-yet-unknown data type to be stored in the array.
template <class T>
void TArray<T>::Gather()
{
    int i;

    for (i = 0; i < size; i++)
    {
	   cout << “Enter a number : “;
	cin >> data[i];
    }
}
4.  The Show() function is used to display the contents of the array to the screen. Apart from the template directive, it is just like a normal C++ member function. Note that a for loop is used to display each item in the array to the screen.
template <class T>
void TArray<T>::Show()
{
    int i;

    for (i = 0; i < size; i++)
	cout << data[i] << endl;
}
5.  The main program creates as object named x and defines the underlying data type to be integer. The array contains five elements; therefore, an array that can contain five integer values is created.
main()
{
    TArray<int> x(5);
    x.Gather();
    x.Show();
    return(0);
}

How It Works

When this program is run, the underlying data type is defined to be of type integer and the array to contain five elements. This information is used to construct an object named x. Next, the member function Gather() is invoked, which prompts the user to enter five integer values. Then the member function Show() is invoked, which displays the five integer values stored in the array to the screen. This is shown in the following screen display:

Enter a number : 44
Enter a number : 23
Enter a number : 87
Enter a number : 12
Enter a number : 29
44
23
87
12
29
Press any key to continue

Try out the program in Listing 6.3. Try entering your own values and verify the program operation.

Listing 6.3 A Template Class Designed to Display an Array of Any Simple Underlying Data Type

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

#include <iostream.h>

template <class T>
class TArray    {
		private:
		       T *data;
		    int size;
		public:
		       TArray(int n);
		    void Gather();
		    void Show();
		};

template <class T>
TArray<T>::TArray(int n)
{
    data = new T[n];
    size = n;
}

template <class T>
void TArray<T>::Gather()
{
    int i;

    for (i = 0; i < size; i++)
    {
	   cout << “Enter a number : “;
	cin >> data[i];
    }
}

template <class T>
void TArray<T>::Show()
{
    int i;

    for (i = 0; i < size; i++)
	cout << data[i] << endl;
}

main()
{
    TArray<int> x(5);
    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.