The objectives for this assignment involve function templates.
Use the assignment project and files provided. There are several overloaded functions for different types of arrays that return the maximum value in the array. Replace (remove) the overloaded function prototypes and definitions with just one function template prototype and definition.
Objectives to be met (Basis for assignment points):
Demonstrate how to prototype (declare) a function template.
Demonstrate how to define a function template. Use the assignment project and files provided. There are several overloaded functions for different types of arrays that return the maximum value in the array. Replace (remove) the overloaded function prototypes and definitions with just one function template prototype and definition.
The existing function call statements should all work the same and produce the same output as they did before calling a template function.
Use the Distance class as is; no modifications are necessary. (Note that when passing a Distance array to the template function, the return value will be a Distance object and therefore cannot be inserted directly into the output stream.)
Review the lab exercises for function and class templates for hints about syntax issues.
Note that an array parameter is a pointer; this works because an array variable is a constant address, and because the array subscript operator [ ] works with a pointer to an array.
If you attempt Recommendation #2 below, do not attempt to pass a SafeArray object to the template function; it is an object, not an array, and will not work with the array subscript operator [ ] inside the template function. It has its own max member function.
Add another function template to print the elements of the arrays to a file. Include at least 2 examples using 2 different array types - print each to a different file. Call the print function somewhere before the array out-of-bounds error at the end of the program listing, or comment-out that line; otherwise the program will exit before the print function can be called. Also, do not attempt to use the Distance array since it will not work with the insertion << operator.
Make SafeArray a class template and demonstrate how to create safe arrays using different types, including the Distance type. Also, write class ArrayIndexOutOfBoundsException in separate .h and .cpp files, and modify the SafeArray class so that it throws this exception instead of printing a message and abnormally ending the program. Use a try-catch with the main program statement that is causing the program to end abnormally so that instead the main program prints the error message but continues to the normal end of program where main returns.
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.