[#16] – Templates – Questions

C-plus-plus-programming-questions-techhyme

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:

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 –

  1. 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.
  2. 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.
  3. Function Template definition begins with a keyword ‘template’ followed type parameters enclosed in angled brackets and then followed by something like normal function.
  4. Function Template can be overloaded either using another function template or non-function template.
  5. Type parameter is a place holder name for the data type used by the function.
  6. A class template is a prescription for creating a generic class in which one or more types are parameterized.
  7. We can use multiple parameters in case of class templates and function templates.
  8. It is possible to give non-type parameters and default arguments in template header.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. 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.
  2. Can we overload a non-function template with the function template ?
    • Yes
  3. 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 – 

  1. What are templates ? Why are they needed ?
  2. Explain the different types of templates ?
  3. What are the function templates ? Write syntax for a function template that takes single type parameter.
  4. Write a function template for calculating minimum number in a given array.
  5. Explain how compiler handles calls to function template ?
  6. How is the function template overloaded ?
  7. Why function templates are preferred over macros ?
  8. Can we specify non-type parameters in function template ? If yes, how ?
  9. What is a class template ? Explain its syntax using suitable example.
  10. What is template instantiation ? Explain.
  11. How does compiler resolves class template objects instantiation ? Give example.
You may also like:

Related Posts

Leave a Reply