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


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

1.  Writing the template class is straightforward; in fact, it’s the same as the one described in How-To 6.1 apart from some altered identifier names to make it look original. The single member variable d is of a yet undefined data type, so there are no problems there. The constructor TNotSimple accepts a single input whose data type will be defined in the main program. The member function Show() is used to send the contents of the member variable d to the screen.
template <class T>
class TNotSimple    {
		private:
		       T d;
		public:
		       TNotSimple(T n);
		    void Show();
		};
2.  The tricky part here actually has very little to do with templates and can be used in any program. The difficulty lies in overloading the insertion operator to output a structure to the screen.
3.  Before the insertion operator is overloaded, a specific structure must be set up. It is called data and holds a string, an integer, and a double as its three fields. Here is the code fragment:
struct data{
	    char name[20];
	    int  age;
	    double height;
	   };
4.  Now that a structure exists, an insertion overload can be defined to deal with that specific data type. This is shown in Figure 6.12.


Figure 6.12  How the insertion overload works.

5.  In the main program, a variable named man is set up and initialized.
struct data man = {“BOBBY”,45,1.98};
6.  The TNotSimple constructor is invoked and its data type defined to that of your user-defined structure. The single input parameter is the variable man, hence the three fields of the structure are passed into the template class.
TNotSimple<data> x(man);

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, it’s only a small step to read data into a structure at runtime. Again, you do not need to take special provisions in the template, it’s 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, it’s 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.


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.