Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 16, Problem 34RQE
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 can specify the “type parameter” instead of using the actual data type.
  • 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.

Example:

For example consider the below function template used to find the cube of given value for any types:

//template function

template <class T>

//function definition of "cube"

T cube(T x)

{

//return the cube of the value

return x*x*x ;

}

Blurred answer
Students have asked these similar questions
C++ Programming   Redesign your class myArray using class templates so that the class can be used in any application that requires arrays to process data. #include <iostream>#include "myArray.h" using namespace std; int main(){myArray list1(5);myArray list2(5); int i; cout << "list1 : ";for (i = 0; i < 5; i++)cout << list1[i] << " ";cout << endl; cout << "Enter 5 integers: ";for (i = 0; i < 5; i++)cin >> list1[i];cout << endl; cout << "After filling list1: "; for (i = 0; i < 5; i++)cout << list1[i] << " ";cout << endl; list2 = list1;cout << "list2 : ";for (i = 0; i < 5; i++)cout << list2[i] << " ";cout << endl; cout << "Enter 3 elements: "; for (i = 0; i < 3; i++)cin >> list1[i];cout << endl; cout << "First three elements of list1: ";for (i = 0; i < 3; i++)cout << list1[i] << " ";cout << endl; myArray list3(-2, 6); cout <<…
Use C++ Convert your Set class in Lab #23 to the template-based class. That is, the template-based set must be able to store data of any data type, not just integers. Write the main function to test your class by creating different sets of different data types. main.cpp: #include <iostream>#include "Set.h"using namespace std;int main() {Set s1;Set s2;s1.insert(1);s1.insert(2);s2.insert(2);s2.insert(3);s2.insert(4);Set result = s1 + s2;result.show();s1 += s2;s1.show();s1 = s1;s1.show();cout << s1.getSize() << endl;cout << s1.getCapacity() << endl;s1.remove(2);s1.show();s1.remove(5);s1.show();return 0;} Set.h: #ifndef SET_H#define SET_H#include "Set.h"Set::Set() {size = 0;capacity = 10;items = new int[capacity];}Set::Set(int cap){size = 0;capacity = cap;items = new int[capacity];}Set::Set(const Set &source){size = source.size;capacity = source.capacity;items = new int[capacity];for (int i = 0; i < size; i++){items[i] =…
In c++ 1) if you are going to make a lot of searches on large number of elements, you should use: a)map b)unordered map c)multi map d)none of the above 2)The [] operator in array template class perform bounds checking -true -false 3)function template allow you to write a single function definition that works with many data types -true -false 4)STL have ........ categories of templates -2 -5 -3 -4

Chapter 16 Solutions

Starting Out with C++ from Control Structures to Objects (9th Edition)

Ch. 16.4 - Prob. 16.11CPCh. 16 - Prob. 1RQECh. 16 - Prob. 2RQECh. 16 - Prob. 3RQECh. 16 - Prob. 4RQECh. 16 - What is unwinding the stack?Ch. 16 - What happens if an exception is thrown by a classs...Ch. 16 - How do you prevent a program from halting when the...Ch. 16 - Why is it more convenient to write a function...Ch. 16 - Why must you be careful when writing a function...Ch. 16 - The line containing a throw statement is known as...Ch. 16 - Prob. 11RQECh. 16 - Prob. 12RQECh. 16 - Prob. 13RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 15RQECh. 16 - Prob. 16RQECh. 16 - Write a function that searches a numeric array for...Ch. 16 - Write a function that dynamically allocates a...Ch. 16 - Make the function you wrote in Question 17 a...Ch. 16 - Write a template for a function that displays the...Ch. 16 - Prob. 21RQECh. 16 - Prob. 22RQECh. 16 - Prob. 23RQECh. 16 - Prob. 24RQECh. 16 - T F All type parameters defined in a function...Ch. 16 - Prob. 26RQECh. 16 - T F A class object passed to a function template...Ch. 16 - Prob. 28RQECh. 16 - Prob. 29RQECh. 16 - Prob. 30RQECh. 16 - Prob. 31RQECh. 16 - T F A class template may not be derived from...Ch. 16 - T F A class template may not be used as a base...Ch. 16 - Prob. 34RQECh. 16 - Prob. 35RQECh. 16 - try { quotient = divide(num1, num2); } cout The...Ch. 16 - template class T T square(T number) { return T T;...Ch. 16 - template class T int square(int number) { return...Ch. 16 - Prob. 39RQECh. 16 - Assume the following definition appears in a...Ch. 16 - Assume the following statement appears in a...Ch. 16 - Prob. 1PCCh. 16 - Prob. 2PCCh. 16 - Prob. 3PCCh. 16 - Prob. 4PCCh. 16 - Prob. 5PCCh. 16 - IntArray Class Exception Chapter 14 presented an...Ch. 16 - TestScores Class Write a class named TestScores....Ch. 16 - Prob. 8PCCh. 16 - Prob. 9PCCh. 16 - SortableVector Class Template Write a class...Ch. 16 - Inheritance Modification Assuming you have...Ch. 16 - Prob. 12PCCh. 16 - Prob. 13PC
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr