C++ quiz
QUESTION 1
[16.1 Function Templates] [Self-Test Exercise 1] [Answer is in the back of the chapter for this and all other self-test exercises.]
Write a function template name 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 (prototype) 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 (prototype) that explains this restriction. Use Preconditions and Postconditions for the comment, similar to the example for the sort function declaration (prototype) on page 711, Display 16.2. Preconditions explains the input (parameters) for the function and the rules for what makes valid parameters. Postconditions explains the output of the function.
QUESTION 2
[16.1 Function Templates]
Write a function template name square. The function takes one values that value * itself. Give both the function declaration (prototype) 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 (prototype) that explains this restriction. Use Preconditions and Postconditions for the comment, similar to the example for the sort function declaration (prototype) on page 711, Display 16.2. Preconditions explains the input (parameters) for the function and the rules for what makes valid parameters. Postconditions explains the output of the function.
______________
QUESTION 3
[16.1 Function Templates]
Would your function template for question 1 work if you gave it the arguments 3 and 4.5? Test it for yourself. If it works, just say yes it works. If it doesn't work, explain why not.
QUESTION 4
[16.1 Function Templates]
Did you know that you can actually write the solution for question 1 in such a way that it would work for more than one type? The book didn't get into this, but for example you could write a function like this:
template<class T, class V>
void compare(T testval1, V testval2);
Just thought I would point this out since I didn't see it in the book. It is often a good practice to *not* allow for two different types unless you have specific reasons to allow for it. Writing this:
template<class T>
void compare(T testval1, T testval2);
Would prevent you from doing: compare(myInt,myDouble); , but it would give you a syntax error which might help you to catch a potential semantic error. If it turns out that the job you wanted to do here is valid, you could do the type-casting explicitly before calling the function rather than taking away the security measure entirely.
Just thought I would point this out. Was this helpful?
QUESTION 5
[16.2 Class Templates]
In order to create a vector of integers, we write:
vector< int > myInts;
Specifically we are creating an object of the vector class template with the type int.
With this example in mind, consider the class template Pair in Display 16.4 on pages 716 and 717. Now write a statement which will create an object named myPair of the Pair class template with the type int. Use the 2nd constructor function with two arguments to create myPair with the initial values 5 and 7.
QUESTION 6
[16.2 Class Templates] [Self-Test Exercise 11] [Answer is in the back of the chapter for this and all other self-test exercises.]
Consider the Pair class template definition and implementation in display 16.4, pages 716 and 717. Also consider the description of the addUp function on page 717.
Give the complete definition for the addUp function for the Pair class template, as it was described in the textbook. The declaration (prototype) for the function is:
template< class T >
T addUp(const Pair< T >& thePair);
QUESTION 7
[16.2 Class Templates]
Write a class template variation of the following class definition.
class basicObject{
public:
basicObject();
basicObject(float newData);
float getData();
void setData(float newData);
private:
float data;
};
Replace all instances of float with the variable class T. You do not need to write implementations for the functions.
QUESTION 8
[16.3 Templates and Inheritance] [Self-Test Exercise 13]
Is it legal for a derived template class to start as shown in the following code? 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 > is omitted, then the compiler may interpret >> to be the extraction operator used for input expressions such as cin >> n; rather than interpreting it as a nested < >.
What would need to be added
QUESTION 9
[16.1 Function Templates]
Consider the following toString function which converts an integer into a string using the ostringstream object type. (For more details on this technique see Chapter 12)
std::string toString(int val){
std::ostringstream outs; //create a string output stream object
outs << val; //insert val onto the string output stream
return outs.str(); //return the string stored on the string output stream
}
Re-write this function as a function template which will convert any data type to a string. Should work for any data type which will work as the right-hand-1side of the << operator (as the integer val is doing here).
QUESTION 10
As a bit of outside reading, please browse some of the class templates available in the Standard Template Library (STL). A nice little reading list can be found here:
http://www.geeksforgeeks.org/the-c-standard-template-library-stl/
Read through a few of these, in particular the Algorithms and Containers.
All of these and more are made possible by class templates.
Describe the things you're most excited to see here and what you might want to use them for. Of course vector is great, but what are some other useful tools and how might you want to use them?
[Name at least 2 that were interesting to you, give some real-life application ideas. Feel free to gush.]
QUESTION 11
Put your mind to it and think of one other example of a good class template to make other than the ones listed in STL and the textbook. Perhaps it could be related to program 2 (I've got at least one idea there, I'll tell you about it next week). Provide a description of it and the types of member functions it would have / the purpose it would serve.
If you can't think of a class template to make, instead tell me an idea you had and what was wrong with it [Why you didn't feel like it would make a good class template afterall.]