A Class Template is not code that is compiled - it is a template from which code is generated. Templates are not (usually) separately compiled. There is no .cpp module for a template class. All of the template information is contained in a header file. Applications needing one or more instantiations of the class template will include the header file.
Templates work well for container classes. They allow a definition of the container without referring to any of the details of the items to be stored in the container. When the class template is used by a client, a template argument will be specified, causing the compiler to generate custom code (for that type of item) from the template. This code is then compiled and linked into the project.
Task 1. Build and run the project. This program (configured for Task 1) allows you to enter some fractions. They are inserted in a List. When you have built the List, the program sums the List contents and displays the sum. You should enter a few fractions and verify that the sum is correctly computed. Exit the program and examine the code to answer these questions:
Examine the following expression found in main in Driver.cpp: cin >> F?
What data type is F? ______________ Explain why the compiler allows an object
of this type as the second operand to a stream extraction operator; also tell
where (in which file) the code to input into this type of object is found.
Regarding the statement: sum+=F;
What function is actually called to perform this operation? (give its prototype):
___________________________________________________________
In what file is the function defined:_________________________________
If sum is 1/2 and F is 1/3, what is the value of the expression "sum+=F"? (Hint - look at the return value of the appropriate method)
___________________________________________________________
Examine the following expression, also found in driver.cpp: cout << Flist;
What data type is Flist? ___________________ Explain why the compiler allows an object of this type as the second operand to a stream insertion operator; also tell where (in which file) the code to input into this type of object is found.
________________________________________________________________________________
________________________________________________________________________________
Task 2. Change the define so TASK 2 is selected. Build the project. An error occurs on this line:
putInList(nameList, aName);
What argument causes the error?____________ What is its data type? ____________
What data type is the function expecting for this argument? _______________
We are going to change this function to a template, so it can handle different types of objects. Add a line above the function definition and change Fraction in the header to whatever. Your code should look like this:
template <class whatever>
void putInList(List & L, const whatever & x){//the rest is unchanged
This is a function template. You learned about these in ICS
(we hope). This will allow the compiler to support the action of putting
any item into a list, regardless of the type of the item. Build the project
again and note that an error is detected for the argument to the method call on
this line:
L.insert(x);
What data type is the argument x? ____________ (Hint: the correct answer is not "whatever"!)
What data type is the insert method expecting? ________________
The List class (as it stands) is designed to handle only a single element type. We used a template to make our putInList function flexible enough to handle any item type; our next task is to make the List class into a class template, so it can handle any type of object. To save you some time, the class template has already been completed for you. You should change the #include in the driver module to include "List_T.h" rather than "List.h". This will cause the templated version of the list class to be used rather than the original, non-templated version.
After making the change, build the project. Oops! An error alerts you to the fact that the class List is now a template. Record the important part of the error message here:
Error: _________________________________________________________________________
To declare an object using a template, you must always include the appropriate template argument list. Objects declared using a template always look like this:
SomeTemplateType <template_argument_1, …, template_argument_n> object;Look at the documentation of the class template definition in the newly included header file.
How many arguments will this template class require? _____
For this task, we want a List object that can hold MyString items. You will have to include MyString as a template argument when you declare a List.
Do this in the putInList function header and the Task 2 code in main. The lines you need to change will look like this when you are done:
void putInList(List<MyString> & L, const whatever & x){
List<MyString> nameList;
Build the project and test. You should be able to create a list of names.
Task 3. Now that we have a class template that works for MyString objects, let's see if it will also work for other data types. Set the TASK variable to 3 and build the project. This will enable both sections of code in main, creating a List of Fractions and a List of MyString's at the same time! Build the project and notice the error in the declaration of FList. You have seen something like this before. Fix the line containing the error and copy the correction here. (This section of code creates a List of Fractions.)
__________________________________________________________
Note the new error. It is the result of a conflict between these lines:
template <class whatever>
void putInList(List<MyString> & L, const whatever & x){
and
putInList(FList, F);Since Flist is a ______________________ (fill in its data type) object,
but the parameter L is a ________________ (fill in its data type), the compiler refuses to make the association! We made a serious error when we "fixed" the List declaration function template for putInList. We specified the first parameter to be a List of MyStrings. Now this allowed us to insert MyString objects into a List of MyString's. But now we have a Fraction and want to insert into a List of Fraction's.
OK - In the header for putInList, change MyString to Fraction and rebuild. The line should look like this:
void putInList(List<Fraction> & L, const whatever & x){
Now we have the same kind of error, but on this line:
putInList(nameList, aName);What we need is for putInList to have a different kind of list each time. Try changing Fraction to something else, like whatever.
void putInList(List<whatever> & L, const whatever & x){
Rebuild. If you are successful, stand up and shout, "I got it!" Then sit
back down and enter some data to be sure the program works.
Task 4. Change the TASK variable to 4 and complete the task outlined at the end of main. Test! When finished, be sure you added your name to the top of Driver.cpp and then obtain a printed copy to be turned in with this lab. No other files need to be submitted with this lab assignment. You should study the template file on your own - you will have to write these by yourself in the near future!
When you have finished the lab, be sure to clean up your work area. Please staple your Driver.cpp listing together with your answer sheet.