Write a comment for the function declaration prototype that


Assignment

QUESTION 1

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

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

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

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
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
void compare(T testval1, T testval2);

Write a class template variation of the following class definition.
classbasicObject{
public:
basicObject();
basicObject(float newData);
floatgetData();
voidsetData(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

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 >
classTwoDimPFArrayBak : 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

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::stringtoString(intval){
std::ostringstream outs; //create a string output stream object
outs< returnouts.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).

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

Considering the examples from the textbook and the examples from the STL, typical uses of class templates can be fairly abstract.

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.]

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Write a comment for the function declaration prototype that
Reference No:- TGS02504017

Expected delivery within 24 Hours