![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Comments If you want to create an array to hold double values, its 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. Dont 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
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.
|
![]() |
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.
|