Machine-translated by Google. Questions are authored in English.
Top

Templates and Generics Questions and Answers

1.

Which of the following defines a family of functions in the same way a class templates defines as family of classes ?

Exam Year(s)

MH SET - 2021, SET Exam - 2021,

Answer: A

A family of functions is defined by Function templates.

In C++ a function template, such as a max function written once with a type parameter T, defines a whole family of functions, one for each type it is instantiated with, just as a class template defines a family of classes.

'Generic functions' describes what templates produce but is not the C++ construct. A virtual function supports run-time polymorphism through a base class, and a friend function is a non-member function granted access to a class's private members; neither defines a family of functions.

Please log in or register to report this question.

Answer: C

The true statement is that templates generate code during compilation.

A C++ template is a blueprint. When it is used with particular types, for example max<int> or vector<string>, the compiler instantiates it, generating a separate concrete function or class for those types at compile time. This is why templates give compile-time (static) polymorphism with no run-time cost.

Run-time polymorphism comes from virtual functions, not templates, so option A is false. Function templates can be overloaded, with each other and with ordinary functions. And templates may take several type parameters, as std::map<Key, Value> shows.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Templates and Generics Questions and Answers || Templates and Generics MCQ Questions and Answers || Templates and Generics GK Questions and Answers || Templates and Generics GK MCQ Questions || Templates and Generics Multiple Choice Questions and Answers || Templates and Generics GK || GK on Templates and Generics || Object Oriented Programming Questions and Answers || Object Oriented Programming MCQ Questions and Answers || Object Oriented Programming GK Questions and Answers || GK on Object Oriented Programming