• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/18

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

18 Cards in this Set

  • Front
  • Back
1. Write a function template named maximum. The function takes two values of the
same type as its arguments and returns the larger of the two arguments (or either
value if they are equal). Give both the function declaration and the function definition
for the template. You will use the operator < in your definition. Therefore, this
function template will apply only to types for which < is defined. Write a comment
for the function declaration that explains this restriction.
1. Function declaration:
template<class T>
T maximum(T first, T second);
//Precondition: The operator < is defined for the type T.
//Returns the maximum of first and second.
Definition:
template<class T>
T maximum(T first, T second)
{
if (first < second)
return second;
else
return first;
}
2. We have used three kinds of absolute value function: abs, labs, and fabs. These
functions differ only in the type of their argument. It might be better to have a function
template for the absolute value function. Give a function template for an absolute
value function called absolute. The template will apply only to types for which
< is defined, for which the unary negation operator is defined, and for which the constant
0 can be used in a comparison with a value of that type. Thus, the function
absolute can be called with any of the number types, such as int, long, and
double. Give both the function declaration and the function definition for the
template.
2. Function declaration:
template<class T>
T absolute(T value);
//Precondition: The expressions x < 0 and -x are defined
//whenever x is of type T.
//Returns the absolute value of its argument.
Definition:
template<class T>
T absolute(T value)
{
if (value < 0)
return -value;
else
return value;
}
3. Define or characterize the template facility for C++.
3. Templates provide a facility to allow the definition of functions and classes that have
parameters for type names.
4. In the template prefix
template <class T>
what kind of variable is the parameter T? Choose from the answers listed below.
a. T must be a class.
b. T must not be a class.
c. T can be only a type built into the C++ language.
d. T can be any type, whether built into C++ or defined by the programmer.
e. T can be any kind of type, whether built into C++ or defined by the programmer,
but T does have some requirements that must be met. (What are they?)
4. e. Any type, whether a primitive type (provided by C++) or a type defined by the user
(a class or struct type, an enum type, or a type defined array, or int, float, double,
etc.), but T must be a type for which the code in the template makes sense. For example,
for the swapValues template function (Display 16.1), the type T must have a correctly
working assignment operator.
5. Display 5.6 shows a function called search, which searches an array for a specified
integer. Give a function template version of search that can be used to search an
array of elements of any type. Give both the function declaration and the function
definition for the template. (Hint: It is almost identical to the function given in Display
5.6.)
5. The function declaration and function definition are given below. They are basically
identical to those for the versions given in Display 5.6 except that two instances of int
are changed to T in the parameter list.
Function declaration:
template<class T>
int search(const T a[], int numberUsed, T target);
//Precondition: numberUsed is <= the declared size of a.
//Also, a[0] through a[numberUsed -1] have values.
//Returns the first index such that a[index] == target,
//provided there is such an index, otherwise returns -1.
Definition:
template<class T>
int search(const T a[], int numberUsed, T target)
{
int index = 0;
bool found = false;
while ((!found) && (index < numberUsed))
if (target == a[index])
found = true;
else
index++;
if (found)
return index;
else
return -1;
}
6. Compare and contrast overloading of a function name with the definition of a function
template for the function name.
6. Function overloading only works for types for which an overloading is provided. (Overloading
may work for types that automatically convert to some type for which an overloading
is provided, but it may not do what you expect.) The template solution will
work for any type that is defined at the time of invocation, provided that the template
function body makes sense for that type.
7. (This exercise is only for those who have already read at least Chapter 6 on structures
and classes and preferably also read Chapter 8 on overloading operators.) Can you use
the sort template function (Display 16.3) to sort an array with base type DayOfYear
defined in Display 6.4?
7. No, you cannot use an array with base type DayOfYear with the template function sort
because the < operator is not defined on values of type DayOfYear. (If you overload <,
as we discussed in Chapter 8, to give a suitable ordering on values of type DayOfYear,
then you can use an array with base type DayOfYear with the template function sort.
For example, you might overload < so it means one date comes before the other on the
calendar and then sort an array of dates by calendar ordering.)
8. (This exercise is only for those who have already read Chapter 10 on pointers and
dynamic arrays.)
Although the assignment operator does not work with ordinary array variables, it
does work with pointer variables that are used to name dynamic arrays. Suppose
that your program defines the template function swapValues as in Display 16.1
and contains the following code. What is the output produced by this code?
typedef int* ArrayPointer;
ArrayPointer a, b, c;
a = new int[3];
b = new int[3];
int i;
for (i = 0; i < 3; i++)
{
a[i] = i;
b[i] = i*100;
}
c = a;
cout << "a contains: ";
for (i = 0; i < 3; i++)
cout << a[i] << " ";
cout << endl;
cout << "b contains: ";
for (i = 0; i < 3; i++)
cout << b[i] << " ";
cout << endl;
cout << "c contains: ";
for (i = 0; i < 3; i++)
cout << c[i] << " ";
cout << endl;
swapValues(a, b);
b[0] = 42;
cout << "After swapping a and b,\n"
<< "and changing b:\n";
cout << "a contains: ";
for (i = 0; i < 3; i++)
cout << a[i] << " ";
cout << endl;
cout << "b contains: ";
for (i = 0; i < 3; i++)
cout << b[i] << " ";
cout << endl;
cout << "c contains: ";
for (i = 0; i < 3; i++)
cout << c[i] << " ";
cout << endl;
8. a contains: 0 1 2
b contains: 0 100 200
c contains: 0 1 2
After swapping a and b,
and changing b:
a contains: 0 100 200
b contains: 42 1 2
c contains: 42 1 2
Note that before swapValues(a, b); c is an alias (another name for) the array a. After
swapValues(a, b); c is an alias for b. Although the values of a and b are in some sense
swapped, things are not as simple as you might have hoped. With pointer variables,
there can be side effects of using swapValues.
The point illustrated here is that the assignment operator is not as well behaved as you
might want on array pointers and so the template swapValues does not work as you
might want with variables that are pointers to arrays. The assignment operator does not
do element-by-element swapping but merely swaps two pointers. So, the swapValues
function used with pointers to arrays simply swaps two pointers. It might be best to not
use swapValues with pointers to arrays (or any other pointers), unless you are very
aware of how it behaves on the pointers. The swapValues template function used with
a type T is only as good, or as bad, as the assignment operator is on type T.
9. Give the definition for the default (zero-argument) constructor for the class template
Pair in Display 16.4.
9. Since the type can be any type at all, there are no natural candidates for the default initialization
values. So this constructor does nothing, but it does allow you to declare
(uninitialized) objects without giving any constructor arguments.
template<class T>
Pair<T>::Pair( )
{
//Do nothing.
}
10. Give the complete definition for the following function, which was discussed in the
previous subsection:
int addUp(const Pair<int>& thePair);
//Returns the sum of the two integers in thePair.
10. int addUp(const Pair<int>& thePair)
{
return (thePair.getFirst( ) + thePair.getSecond( ));
}
11. Give the complete definition for the following template function, which was discussed
in the previous subsection:
template<class T>
T addUp(const Pair<T>& thePair);
//Precondition: The operator + is defined for values of type T.
//Returns the sum of the two values in thePair
11. template<class T>
T addUp(const Pair<T>& thePair)
{
return (thePair.getFirst( ) + thePair.getSecond( ));
}
12. What do you have to do to make the following function a friend of the template class
PFArray in Display 16.5?
void showData(PFArray<T> theObject);
//Displays the data in theObject to the screen.
//Assumes that << is defined for values of type T.
12. You add the following to the public section of the template class definition of PFArray:
friend void showData(PFArray<T> theObject);
//Displays the data in theObject to the screen.
//Assumes that << is defined for values of type T.
You also need to add a function template definition of showData. One possible
definition is as follows:
namespace PFArraySavitch
{
template<class T>
void showData(PFArray< T > theObject)
{
for (int i = 0; i < theObject.used; i++)
cout << theObject[i] << endl;
}
}//PFArraySavitch
13. Is it legal for a derived template class to start as shown below? The template class
TwoDimPFArrayBak is designed to be a two-dimensional partially filled array with
backup.
template<class T>
class TwoDimPFArrayBak : public PFArray< PFArray<T> >
{
public:
TwoDimPFArrayBak( );

Note that the space in < PFArray<T> > is important, or at least the last space is. If
the space between the next-to-last > and the last > is omitted, then the compiler
may interpret >> to be the extraction operator used for input in expressions like cin
>> n; rather than interpreting it as a nested < >.
13. Yes, it is perfectly legal. There are other, possibly preferable, ways to accomplish the
same thing, but this is legal and not even crazy.
14. Give the heading for the default (zero-argument) constructor for the class
TwoDimPFArrayBak given in Self-Test Exercise 13. (Assume all instance variables are
initialized in the body of the constructor definition, so you are not being ask to do
that.)
14. template<class T>
TwoDimPFArrayBak<T>::TwoDimPFArrayBak( )
: PFArray< PFArray<T> >( )
Using ___ ___, you can define functions that have a parameter for a
type.
function templates
Using ___ ___, you can define a class with a type parameter for subparts of
the class.
class templates
The predefined vector and basic_string classes are actually ___ ___.
template classes
You can define a template class that is a derived class of a template base class.
true