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


Comments

If you want to create an array to hold double values, it’s just a question of changing int to double in the construction of x. This is shown in the next program fragment. All you need to do is change that single line in Listing 6.3.

main()
{
    TArray<double> x(5);
    x.Gather();
    x.Show();
    return(0);
}

If you want to increase the number of items stored in the array, simply change the 5 to whatever value you require, say 12. Don’t get too adventurous and use huge numbers here; it will take you ages to type in lots of values.

main()
{
    TArray<double> x(12);
    x.Gather();
    x.Show();
    return(0);
}

6.4 Write a template class that has two undefined data types that can be resolved at a later time?

Problem

So far, you have dealt with templates that have a single undefined data type that is resolved in the main program. In practice, it is common to have more than one unknown in the template because several data types are often involved in any single program.

Technique

The way around the problem is straightforward. The previous three How-Tos dealt with templates that described a single simple data type and a single array data type. The example in this How-To deals with how two simple variables can be incorporated into a class template. Previously, the symbol T was used to describe the unknown; now the symbols T1 and T2 are used to describe the two unknowns.

Steps

1.  First of all, create a template that can hold two simple data types. Define them as T1 and T2 (see Figure 6.8).


Figure 6.8  The template class definition for two simple variables.

2.  The next step is to define the constructor that can be configured to translate into any two simple data types (see Figure 6.9).


Figure 6.9  The template constructor definition for two simple variables.

3.  The definition of the member function Show() is just like a normal C++ member function, except for the distinguishing feature of the template directive for two undefined data types at its start (see Figure 6.10).


Figure 6.10  The template member function definition for two simple variables.

4.  When the template class is called into action, you specify the two simple data types to be used. This example uses integer and double, but any combination is legal as long as the actual values used match the declaration (see Figure 6.11).


Figure 6.11  Template class A configured to hold an integer and a double.

How It Works

This is a basic example of how to get two items of data not necessarily of the same data type into a template class. The program does nothing else except that task. The template is set up as described earlier in this How-To. The data type of two member variables, a and b, is designated in the main program. Here the constructor collects the data, creates an object called x, and defines the two member variables to be of type integer and double, respectively. The Show() member function displays the values to the screen to prove that everything worked. When I ran the program in Listing 6.4a I got the following output:

5
3.5
Press any key to continue

Listing 6.4a How to Define Two Variables in a Template Class and Define Their Actual Data Type in the Main Program

// Template using two simple data types.

#include <iostream.h>

template <class T1, class T2>
class A    {
	private:
	    T1 a;
	    T2 b;

	public:
	    A(T1 n, T2 i);
	    void Show();
	};

template <class T1, class T2>
A<T1, T2>::A(T1 n, T2 i)
{
    a = n;
    b = i;
}

template <class T1, class T2>
void A<T1, T2>::Show()
{
    cout << a << endl;
    cout << b << endl;
}

main()
{
    A<int, double> x(5,3.5);
    x.Show();
    return(0);
}

As an additional example, take a look at a program that finds the result of a number raised to a power. The full code is given in Listing 6.4b. It is very similar to Listing 6.4a with the addition of a member function called Calc(). In the main program, the template data types are configured to be double and double; then the values 5 and 3.5 are applied to the constructor. The member function Calc() does the calculation and stores the result in the member variable result. The Show() member function displays the member variable result to the screen.

When I ran my program with the stated inputs I got the following output:

279.508
Press any key to continue

Listing 6.4b A Template Class That Raises a Number of Any Simple Data Type to a Power of Any Simple Data Type

// Template using two simple data types.

#include <iostream.h>
#include <math.h>

template <class T1, class T2>
class A    {
	private:
	    T1 a;
	    T2 b;
	    double result;
	public:
	    A(T1 n, T2 i);
	    void Calc();
	    void Show();
	};

template <class T1, class T2>
A<T1, T2>::A(T1 n, T2 i)
{
    a = n;
    b = i;
}

template <class T1, class T2>
void A<T1, T2>::Calc()
{
    result = pow(a,b);
}

template <class T1, class T2>
void A<T1, T2>::Show()
{
    cout << result << endl;
}

main()
{
    A<double, double> x(5,3.5);
    x.Calc();
    x.Show();
    return(0);
}

Comments

You can have as many undefined data types as you want in a class template; however, if you use too many, the code begins to get unwieldy and difficult to use. Try to keep it simple. As a further example, the following is a template class with four unknowns:

template <class T1, class T2, class T3, class T4>
class B    {
	private:
	    T1 a;
	    T2 b;
	    T3 c;
	    T4 d;
	    double result;
	public:
	    A(T1 n, T2 i, T3 x, T4 y);
	    void Calc();
	    void Show();
	};

A typical constructor would look like this:

template <class T1, class T2, class T3, class T4>
A<T1, T2, T3, T4>::A(T1 n, T2 i, T3 x, T4 y)
{
    a = n;
    b = i;
c = x;
d = y;
}

And so the pattern continues.


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.