www.freeOnlineBooks.tk
part of www.asadali.tk
Download Books for Free
Read Books OnlineTesting the Template Classes 18 3
7. In the code editor of your choice, reopen the
source file for the code you just created.
In this example, the file is named ch31.cpp,
although you can use whatever you choose.
2. Append the code from Listing 31-2 to your file.
Better yet, copy the code from the source file on
this book's companion Web site.
Listinc 31-2: THE Test DRIVER FOR THE TEMPLATED CLASS
EXAMPLE
int main()
(
printf("Creating base\n"};
Base fooBase;
printf("Creating template as member\n");
TemplateAsMember tempMem;
printf("Creating template as base\n");
TemplateAsBase tempBase;
printf("Creating template as base tem-
plate\n");
Temp lateAsBaseTemplate< Foo, AnotherBase>
tempAsBT;
fooBase.Print(}:
tempMem.Print();
tempBase.Print():
tempAsBT.Print():
return 0;
3. Save the source file in your code editor and
close the code editor.
4, Compile the source code with the compiler of
your choice, on the operating system of your
choice.
When the program is run, if you have done every-
thing properly, you should see the following output
in the shell window:
Creating base
Void constructor called
Creating template as member
Creating base with name
[Temp] ateAsMember]
Creating template as base
Creating base with name [TemplateAsBase]
Creating template as base template
Creating base with name
[Temp] ateAsBaseTemplate]
Base:
Name = Nothing
Pointer =
Pointer is NULL >1
TemplateAsMember
Base:
Name = TemplateAsMember
Pointer =
Pointer is NULL
TemplateAsBase:
Base:
Name = TemplateAsBase
Pointer =
Pointer is NULL
Base:
Name = TemplateAsBaseTemplate
Pointer =
Pointer is NULL
Another pointer is NULL
The output from this program shows us that each of
the various template instantiations works. As you
can see, in each case (see, for example, the line
marked — 1), the constructor was called and the
various member variables assigned proper default
values. Looking at the examples, it should be clear
that each of the various methods arrives at the same
conclusion.
Concrete classes that have been made into
templates as a specific form of a class are best
suited for extension. This is to say, if you have
a template class that accepts a particular type
of class for its argument, you are better off
extending your template class by creating a
form of it as a specific class — and then deriv-
ing from that specific class. The reason for this
is more human than technical: People usually
don’t think in terms of templates so much as
in terms of class names.184
Technique 31: Extending a Template Class
Using Non-class Template
Arguments
Looking at the four choices in extending base
classes, you will probably notice a few things that
suggest particular approaches to the process.
To utilize methods in a base class being used as a
template, you must specify which version of the
class the template is to use. The reason is that you
could conceivably have a class that inherited from
multiple classes of the same template, with different
types associated with it. This is a good thing because
it allows you to segregate speci functionality into
separate classes.
Another thing worth noticing is that you may create
a templated class that does not require a class as its
argument. For example, we could create a template
with a numeric argument; the following steps show
you how:
1, In the code editor of your choice, create a new
file.
In this example, the file is named ch3La.cpp,
although you can use whatever you choose.
2. Type the code from Listing 31-3 into your file.
ListinG 31-3: A TEMPLATE CLASS WITH A NON-CLASS
(ARGUMENT
template
class LessThanTen
(
A _element;
public:
LessThanTen( A entry )
' set ( entry );
LessThanTent const LessThanTen& aCopy }
set( aCopy._element);
votd set( A value )
i
33
if ( value > O && walue < 10 )
element = value;
72
t
A gett)
|
return _element;
b
This code works for a class argument, so long as.
that class can be compared to an integer. It can
also be used for a non-class argument, such as an
integer, long integer, or even a floating point
value.
With this class shown in Listing 31-3, there is no
reason that the template argument should be a
class. In fact, it would be implied that a numeric
element was used, since the value which is
passed into the set method is compared to an
integral value of 10 (see the line marked — 2).
Add the following code to your source file to
test the integer class. This code will test the
template class we just defined above.
int main(void)
{
LessThanTen¢int> ten(3)+ >5
printf("The value is &d\n",
ten.get() J:
ten.set( 23 ); za
printf("The value is now d\n",
ten.get() 3;
return 0;
t
Save the source file in your code editor and
then close the code editor.
Compile the source code with the compiler of
your choice, on the operating system of your
choice.
When the program is run, if you have done every-
thing properly, you should see the following output
in the shell window: