Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  1. 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.
  2. 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.

  1. 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.
  2. 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago