![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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 couldnt 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).
Steps
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); }
|
![]() |
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.
|