Question: Lab Assignment Objectives Define a function template to be used as a tool for creating a set of functions that have the same code logic
Lab Assignment Objectives
- Define a function template to be used as a tool for creating a set of functions that have the same code logic but whose code can be applied to different data types.
- Separate interface and application files for a template function.
Function Templates
Function templates allow the programmer to define a function whilst deferring the definition of the types until the program is completed. How this works is that when the program is compiled, the compiler creates as many versions of the functions as there are function calls with different types. The syntax included the reserved words template and typename in angle brackets. To facilitate code reuse, function templates are placed in an interface file that is included in the program being compiled.
Understand the Application
Write a templated function to find the index of the smallest element in an array of any type.
Test Run Requirements:
Test the template function with six arrays: two of type int, two of type double and two of type char. Print the value of the smallest element in each array. Use the sample test cases shown below for part of your test run verification. Be sure to include the additional test cases required to complete your test specification requirements.
- Ensure that your solution is well organized. Provide a program header and comments to document and organize your source code. User defined function(s) need be documented.
- Provide a statement in your program header to state how many versions of the template function your compiler needed to generate at run time to accomplish the specification test run requirement. Include the reason for your answer.
Sample Output
Here is an example of a partial run sample:
/* ------------------- Sample run ----------------------------- Smaller of x and A: A Smaller of 149 and 182: 149 Smaller of 56.7 and 11.3: 11.3 --------------------------------------------------------------- */
Additional Requirements
- How many versions of the template function does your compiler need to generate at run time to accomplish the specification test run requirement?
- Personalize the nameguard on your header file.
- a4.h : interface file
- a4.cpp : application file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
