|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
6.5 Use a template class to handle a structure?Problem The problem with structures is that they are a user-defined data type and therefore have an infinite number of possible definitions. At first glance, it seems impossible to account for every possible combination in a template. Technique The solution is incredibly easy. You have already seen how to create a template to handle any simple data type. The template to handle a structure is the same as the one that handles a simple data type. The trick lies outside the template. The insertion operator is overloaded to handle the user-defined structure and the template is used to send the data held in the structure to cout. Steps
How It Works The program in Listing 6.5a defines a structure named data. This code overloads the insertion operator to deal with that structure. The template class TNotSimple is very simple and is the same as the one shown in How-To 6.1. A constructor and a member function are declared to allow manipulation of the data. In the main program, you stamp the data type struct data on the template class and offer the fields contained in the internal variable man as input to the object x. To prove things have really worked, the Show() function displays the contents of the class to the screen. My output looks like this: Structures, Template Classes And Constant Data. BOBBY 45 1.98 Press any key to continue. Listing 6.5a This Template Class Can Also Handle Structures Through the Process of Overloading // Template using single data item. #include <iostream.h> #define tab \t struct data{ char name[20]; int age; double height; }; ostream& operator << (ostream& str_out, data& q) { str_out << q.name << tab; str_out << q.age << tab; str_out << q.height << endl; return(str_out); }; template <class T> class TNotSimple { private: T d; public: TNotSimple(T n); void Show(); }; template <class T> TNotSimple<T>::TNotSimple(T n) { d = n; } template <class T> void TNotSimple<T>::Show() { cout << d << endl; } main() { struct data man = {BOBBY,45,1.98}; TNotSimple<data> x(man); x.Show(); return(0); } Having seen how to output a structure containing constant data from a template class, its only a small step to read data into a structure at runtime. Again, you do not need to take special provisions in the template, its just a question of overloading the extraction operator to handle a specific user-defined structure. Here is the code fragment that performs the required action: istream& operator >> (istream& str_in, data& q) { cout << Enter name : ; str_in >> q.name; cout << Enter age : ; str_in >> q.age; cout << Enter height : ; str_in >> q.height; return(str_in); }; With this overload defined, its just a question of adding a member function that I have called Gather().Gather() reads in data to the member variable d, which has been set up as the data type of the structure. When I ran my program, I got the following output: Template class constructed Enter name : MAGGIE Enter age : 21 Enter height : 1.64 MAGGIE 21 1.64 Press any key to continue Listing 6.5b shows the program in full. Listing 6.5b Overloading Both the Insertion and Extraction Operator Allows a Template to Handle Runtime Data Entry // Template using single data item. #include <iostream.h> #define tab \t struct data{ char name[20]; int age; double height; }; ostream& operator << (ostream& str_out, data& q) { str_out << q.name << tab; str_out << q.age << tab; str_out << q.height << endl; return(str_out); }; istream& operator >> (istream& str_in, data& q) { cout << Enter name : ; str_in >> q.name; cout << Enter age : ; str_in >> q.age; cout << Enter height : ; str_in >> q.height; return(str_in); }; template <class T> class TNotSimple { private: T d; public: TNotSimple(); void Gather(); void Show(); }; template <class T> TNotSimple<T>::TNotSimple() { cout << Template class constructed << endl; } template <class T> void TNotSimple<T>::Gather() { cin >> d; } template <class T> void TNotSimple<T>::Show() { cout << d << endl; } main() { TNotSimple<data> x; x.Gather(); x.Show(); return(0); } Comments You are at liberty to set up a user-defined structure and pass it to a template class as if it were a simple data type. In that respect, you can treat structures and simple data types as the same thing.
|
![]() |
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.
|