![C-plus-plus-programming-questions-techhyme](https://techhyme.com/wp-content/uploads/2021/07/C-plus-plus-programming-questions-techhyme-1024x677.jpg)
Templates allow us to create a single function or a class for a group of similar functions or classes in a generic manner. The template declared for function is called a function template and those declared for classes is called a class template.
Index:
- [#1] – Introduction to C++ – Questions
- [#2] – Preparing and Running C++ Program – Questions
- [#3] – C++ Fundamentals – Questions
- [#4] – Data Input and Output – Questions
- [#5] – Control Structures – Questions
- [#6] – Functions – Questions
- [#7] – Arrays and Strings – Questions
- [#8] – Structures and Unions – Questions
- [#9] – Pointers – Questions
- [#10] – Object and Classes – Questions
- [#11] – Constructors and Destructors – Questions
- [#12] – Operator Overloading and Type Conversion – Questions
- [#13] – Inheritance – Questions
- [#14] – Virtual Functions – Questions
- [#15] – C++ Streams and Files – Questions
- [#16] – Templates – Questions
- [#17] – Exception Handling – Questions
For example – we can create a template for function cube() that calculates the cube of any data type including int, float, double etc. Templates are the fundamental tool for generic programming in C++, whose main purpose is to write reusable code. Template was originally not the part of C++ and was added later.
Key Points To Remember –
- Templates are the fundamental tool for generic programming in C++, Whose main purpose is to write reusable code. Template enables us to specify a prototype of related functions called function templates and a prototype of related classes called class templates.
- Function Templates are the generic functions which can handle different data types without the need of writing separate code for each of them. Thus it reduces lot of manual effort in development, testing and debugging of programs.
- Function Template definition begins with a keyword ‘template’ followed type parameters enclosed in angled brackets and then followed by something like normal function.
- Function Template can be overloaded either using another function template or non-function template.
- Type parameter is a place holder name for the data type used by the function.
- A class template is a prescription for creating a generic class in which one or more types are parameterized.
- We can use multiple parameters in case of class templates and function templates.
- It is possible to give non-type parameters and default arguments in template header.
Viva Voce Questions –
Here are commonly asked questions with answers:
- What is the difference between the function template and template function ?
- When we define a template then it is called function template and when the code is generated on function call, then it is called template function.
- Can we overload a non-function template with the function template ?
- Yes
- What happens when call to the function template is made ?
- When compiler encounter the function call then the specified data type is substituted in the template definition and internally creates a specific version of the function of that type.
Other Similar Questions –
- What are templates ? Why are they needed ?
- Explain the different types of templates ?
- What are the function templates ? Write syntax for a function template that takes single type parameter.
- Write a function template for calculating minimum number in a given array.
- Explain how compiler handles calls to function template ?
- How is the function template overloaded ?
- Why function templates are preferred over macros ?
- Can we specify non-type parameters in function template ? If yes, how ?
- What is a class template ? Explain its syntax using suitable example.
- What is template instantiation ? Explain.
- How does compiler resolves class template objects instantiation ? Give example.
- 80 Most Important Network Fundamentals Questions With Answers
- 100 Most Important SOC Analyst Interview Questions
- Top 40 Cyber Security Questions and Answers
- Top 10 React JS Interview Theory Questions and Answers
- CISSP – Practice Test Questions – 2024 – Set 20 (53 Questions)
- Part 2: Exploring Deeper into CCNA – Wireless (145 Practice Test Questions)
- Part 1: Mastering CCNA – Wireless (145 Practice Test Questions)
- [1z0-1085-20] Oracle Cloud Infrastructure Foundations 2020 Associate MCQ Questions – Part 3
- [1z0-1085-20] Oracle Cloud Infrastructure Foundations 2020 Associate MCQ Questions – Part 2
- [1z0-1085-20] Oracle Cloud Infrastructure Foundations 2020 Associate MCQ Questions – Part 1