Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 16, Problem 26RQE
Explanation of Solution
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 indentifies 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 the specified value:
//Include the required header files
#include<iostream>
#include<string>
using namespace std;
//template prefix
template <class T>
//function template definition of ...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
c++
Here are struct data members:
integer for the ID of a student
character array for the name. The size of the array is 15
floating-point numbers for the GPA
an integer for a student status
Write the declaration of the struct, lines of code to dynamically allocate size of the struct defined previously, the user will be asked to enter the size of the array, and a function that receives an array of the struct, the length of the array and the student ID as parameters. It should return the name, GPA and student status if found. Make sure to address if not found.
C++ Code Dynamic Arrays
A function template can be overloaded by another function template with the samefunction name. T/F
Chapter 16 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Ch. 16.1 - Prob. 16.1CPCh. 16.1 - Prob. 16.2CPCh. 16.1 - Prob. 16.3CPCh. 16.1 - Prob. 16.4CPCh. 16.1 - Prob. 16.5CPCh. 16.3 - Prob. 16.6CPCh. 16.3 - The following function accepts an i nt argument...Ch. 16.3 - Prob. 16.8CPCh. 16.3 - Prob. 16.9CPCh. 16.4 - Prob. 16.10CP
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
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
- What must you be sure of when passing a class object to a function template that uses an operator, such as * or >?arrow_forwardTemplate parameter names among template definitions must be unique. T/Farrow_forwardCLASS TEMPLATE - ARRAY Class Template – Array Arya has completely learned about function templates and using them in Arrays, her teacher has given her a task to implement class Templates for all the same things-searching, sorting, and displaying an array using templates. You are her friend and you have knowledge about class templates, help Arya to make a class template to search, sort, and display an array.Write a C++ program to create a class template using an array. Perform operations like sorting, searching, and displaying the array using a class template.Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, member variable names, and function names should be the same as specified in the problem statement.A class named Array with the following member variables. Data Type Variable Name Integer size T* array Define the following public functions inside the class Array. Function Name Description void sorting()…arrow_forward
- In C++Using the code provided belowDo the Following: Modify the Insert Tool Function to ask the user if they want to expand the tool holder to accommodate additional tools Add the code to the insert tool function to increase the capacity of the toolbox (Dynamic Array) USE THE FOLLOWING CODE and MODIFY IT: #define _SECURE_SCL_DEPRECATE 0 #include <iostream> #include <string> #include <cstdlib> using namespace std; class GChar { public: static const int DEFAULT_CAPACITY = 5; //constructor GChar(string name = "john", int capacity = DEFAULT_CAPACITY); //copy constructor GChar(const GChar& source); //Overload Assignment GChar& operator=(const GChar& source); //Destructor ~GChar(); //Insert a New Tool void insert(const std::string& toolName); private: //data members string name; int capacity; int used; string* toolHolder; }; //constructor GChar::GChar(string n, int cap) { name = n; capacity = cap; used = 0; toolHolder = new…arrow_forward4. Absolute Value Template Write a function template that accepts an argument and returns its absolute value. The absolute value of a number is its value with no sign. For example, the absolute value of -5 is 5, and the absolute value of 2 is 2. Test the template in a simple driver program being sure to send the template short, int, double, float, and long data values. SAMPLE RUN #4: ./AbsoluteValueTemplate Hide Invisibles Highlight: None Show Highlighted Only O Interactive Session Enter.a short.or.just.enter to-move.to.next.data type:-124 The absolute. value of.-12.is: 124 Enter.a short.or.just.enter to-move to. next data type:-11- The absolute value of.-11·is: 11e Enter.a short.or.just.enter to-move.to.next.data -type:-84 The absolute value of. -8.is: 84 Enter.a short.or.just.enter to-move to-next.data type:88e The absolute value of. 88 -is: 88e Enter.a short.or.just.enter to-move.to.next data type:45e The absolute value of.45.is: 45- Enter.a short.or.just.enter to-move -to-next.data…arrow_forwardTranslator Using as a basis the program that allows to deal cards from a deck to a player's hand, add functionality that allows to update the deck of cards. Updating the deck of cards involves moving the cards from the deck to the beginning of the vector as the player requests a new card. At each deal, the cards in the deck must be printed to make the code in c language. The following functions must be implemented additionally: updateDeck. - This function receives two arguments: a vector with the deck of cards to update and the current size of the deck of cards. It will move the cards in the deck to the beginning of the vector. printVector - This function will receive two parameters: a vector and its dimension. The function will print the vector, and will print the values (the vector) of the initial hand, the updated deck, and the final hand. to make the code in c language Expected output :arrow_forward
- c++ pleasearrow_forwardStatic analyzer write code c++ create your static analyzer tool thatreads your code as a text. Then, it analyzes it based on the below checklist. The checklist: - Do the attributes (e.g., data type and size) of each parameter match theattributes of each corresponding argument?arrow_forwardC++ Program You may use the vector and iostream libraries. You are allowed to use three built-in vector member functions (and no others) but you may not have to use them all. The member functions you are allowed to use are size(), at(), and push_back() Write a function that outputs a vector of doubles with each number in the vector separated by a space then a newline after the entire vector is output. The whole vector output should be preceded by a single line saying "Current Vector Contents:". Write a function that takes a vector a doubles and reverses the order of all the elements of the vector. Write a function that fills a vector of doubles with positive numbers using the standard input stream cin, terminate the input when the user enters any negative number. A single output prompt should precede the initial input stating directions for user. Write a main function that creates an empty vector, calls functions from 2 & 3 and calls your output function before and after each…arrow_forward
- TRUE OR FALSE: In C++, comments that begin with the "multi-line" comment symbol can't be nested. A member function of a class has access to only the public data members of the class. If a C++ function does not take any parameters, parenthesis around the empty parameter list are still required. The size of a pointer variable depends on the type that it is pointing to.arrow_forwardTopic: pointers, dynamic array and command line arguments Write a complete C++ program named “showHelp” that accepts command line arguments. It checks whether there is a command line argument of “/help” or“/?” or “-help” followed by a topic number. It will print out “yes, topic number(<number>) if there is one. Otherwise, it prints out “no, topic number(N/A) For example, if you run this program with correct arguments as follows, it willprint out yes and its associated topic number respectivelyshowHelp /? 101showHelp /debug /help 102showHelp /print /help 103 /verboseshowHelp -verbose -debug -help And if you run the program with invalid arguments, it will print no, in all casesshowHelp -helpshowHelp 101 /?showHelp 101 102 /help /verboseshowHelp /? /help -helpNote: command line arguments are simply an array of pointers to C-string. example code: #include <iostream> using namespace std; int main(int argc, char** argv){...}arrow_forwardPROGRAMMING LANGUAGE: C++ You need to store hiring date and date of birth for teachers, and for students store their admission date and date of birth. You need to create a Date class for this purpose. Create objects of Date class in Teacher and Student class to store respective dates. You need to write print function in Teacher and Student classes as well to print all information of Teachers and Students. You need to perform composition to implement this task. Create objects of Teacher and Student classes in main function and call print function for both objects. Print Date class here. Print updated Teacher class here. Print updated Student class here. Print main function here. Print Outputshere.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr