Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 17.1, Problem 6STE
Program Plan Intro

Function template:

In C++, a function template is referred as a “generic” function, which can work with different data types.

  • While writing a function template, a programmer should use a “type parameter” to denote a “generic” data type instead of using the actual parameter.
  • The compiler generates the code, when it encounters a function call to a function template. This code will handle the particular data type which is used in the function call.
  • The compiler identifies the argument type and generates the code to work with those types.
  • The generated code is referred as “template function”.

Example:

For example consider the following function template for finding a cube of value:

  template <class T>

  T cube(T x)

  {

  return x * x * x ;

  }

  • A function template must begin with the keyword “template” and it is followed by a pair of angle brackets, which contains one or more “generic” data types.
  • A “generic” type must start with the keyword “class”, followed by an argument name which stands for the data type.
  • The statement “T cube(T x)” is referred as function header, where “T” is a “type parameter”, “cube” is the function name and the variable “x” is declared for the type “T”.

Blurred answer
Students have asked these similar questions
Using C++ make a set of function templates that have these features: This function must return a value. A function template with 1 template parameter, T. And, any other parameters you want. A function template with 2 template parameters, T1 and T2. And, any other parameters you want. Using your own creativity, make a set of class templates that have these features: For this class template, put everything in one place--do not declare the member functions and have separate definition of the member functions elsewhere. Keep them in one place. Include a private variable. Include a constructor that loads the private variable when constructed. Include a destructor that clears the private variable to zero. Include set and get functions to set and get the private variable. For this class template, use declarations for variables and functions, like you do in header file (which you may use if you want). Then, separately put the full function definitions for each class member function…
READ THE QUESTION CAREFULLY AND DO AS IT SAYS IN QUESTION . MATCH OUTPUT AND INPUT AS IT IS.     ------------------------------------------------ Template Specialization in c++ is used to write generic code. Template code is written once and use for any data type including user-defined data types.Example: sort() can be written and used to sort any data type items. Create a Template code that reads input from the user, user can enter any type of data like integer, double, float.sum() is the template method that can take any two types of data and add it and return the sum of the two numbers.Create a Template Specialization method to add two integer values only.If a specialized version is present, the compiler first checks with the specialized version and then the main template.  Sample Input  and Output:Enter two integer Numbers: 55Enter two float Numbers: 3.53.1Enter two double Numbers: 3.68.6Sum of two integer Numbers: only integer10Sum of two float Numbers: 6.6Sum of two double…
Computer Science Write the definition of the class template ArrayListType, and the derived class unordered ArrayListType. Provide implementations for the non abstract functions in the base template class, and implementations of the template functions in the derived template class. Write a test program to test the functions in the derived template class. Instantiate objects of type derived class in your test program using template parameters of type float, int.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning