Template Classes


An example of a construct called link lists is the following:

image from book

 class alist {    private:       aType an_object;      alist * node;    public:      ..... }; 

image from book

This is an example of a container class. Further examples of container classes are:

image from book

 stacks // These constructs are        // what are called Last In First Out (LIFO) queues // These constructs are        // what are called First In First Out (FIFO) 

image from book

Our interest in this section is how to create general classes for lists, stacks and queues. Container classes will be discussed briefly here and more in a later lecture.

The general construct of a template class is:

image from book

 template <typename Atype> class Aclass {    ..... }; 

image from book

where Atype is the unspecified data type of some of the attributes that appear in the body of the class definition.

The methods of the class are template functions but they do not need to be declared such in the class' definition. However, if the methods are defined outside of the class definition, then they must be declared as template functions by providing the appropriate template function header in the definition.

When you define an object of a template class, you would use a statement like the following:

image from book

 Aclass<Atype> an_object; 

image from book

where Atype is the data type specified at runtime and Aclass<Atype> becomes the name of the class.

For example see: genclas1.cpp. Notice in this example that the keyword template has to appear in the definition of each of the class' methods when they are defined outside of the class but it does not appear on the prototype inside of the class definition. If the methods had been defined within the class, the template header would not have been included in the definition.

It is also possible to define template classes that involve multiple data types. There may be more than one and they are separated by a comma. For example:

image from book

 template <class Atype1,class Atype2,,class Atypen > class Aclass {       .... }; 

image from book

For example see: genclas2.cpp.

Above in the discussion of template functions, the keyword typename was substituted for the keyword class in the header of the template class definition. This is also possible for template classes as well. See genclas4.cpp. If your compiler will permit this syntax, you should use this method of template class definition because it will become the standard in the future if not already.

Similar to default values in functions, template classes may have default types in their header. The template class header could look something like the following:

image from book

 template <class Atype=float, class Btype=int> class stuff {    .... }; 

image from book

See genclas6.cpp. In this example, you should pay particular attention to the definition of the objects: object1, object2, object3 and object5 in comparison with the definition of object4:

image from book

 stuff<int, double> object1(5, 5.25);            // The default data types                                                     // are overruled. stuff<char, char*> object2('A', "PBCC");         // The default data types                                                     // are overruled. stuff<double, float> object3(25.36, -14.17F);   // The default data types                                                    // are overruled. stuff<> object4(34.23F,345);                    // The default data types                                                    // are NOT overruled. ratio fraction1(45,67); stuff<ratio,double> object5(fraction1,56.76); // The default data types                                              // are overruled. stuff<double> object6(3.44,7);               // Only one of the default                                              // data types is overruled. 

image from book

 Note:  Although default template classes are currently permitted, default template functions are not currently allowed.

In the study of template functions an explicit specialization of the template function was created. In Standard C++ it is also possible to create explicit specializations of template classes as well. See genclas5.cpp. However, while it does work on Visual Studio .NET, you may find that this program when tried on your compiler does not compile. Apparently this type of template class has not yet been implemented on some compilers. Try it on another compiler.

Template classes can be used to solve a lot of problems. For example, some of the problems caused by arrays can be handled using template classes. That is a traditional C++ array may have its index go out of bounds and the programmer must prevent this from happening. The following example using template classes shows a solution for this problem with arrays genclas3.cpp.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net