Technical Interview Questions and Answers :: C++
1. Virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. We cannot have virtual constructors but we can have virtual destructors.
6. If a virtual function is defined in the base class. It need not be necessarily redefined in the derived class.
Templates execute us to define generic class and functions and thus provides support for generic programming. It is an approach where generic types are used as parameters in algorithms so that they work for a variety of suitable data types and data structures.
Templates are defined with a parameter that would be replaced by a specified data type at the time of actual use of the class or function, the template is sometimes called as parameterized classes or functions.
A class created from a class template is called a template class. The syntax for defining an object of a template class is :
The process of creating a specific class from a class is called instantiation. The compiler will perform the error analysis only when an instantiation takes place.
We can use more than one generic data types in a class template. They are declared as a comma separated list within the template specification as shown below:
---------- ( body of the class)
Like class template, we can also define function templates that would be used to create a family of function with different argument types.
return type function name(argument of type T)
//body of function
//with type T
We must use the template parameter T as and when necessary in the function body and in its arguments list.
Like template class we can use more than one generic data type in the template statement, using comma separator
return type function name(argument of type T1,T2,â€¦)
//body of function
A template function may be overloaded either by template function or by the ordinary function of its name. in such cases, the overloading resolution is accomplished by
â€¢ Call an ordinary function that has an exact match.
â€¢ Call a template function that would be created with an exact match.
â€¢ Try normal overloading resolution to ordinary function and call the one that match.
The member functions of the template classes themselves are parameterized by the type argument and therefore the function must be defined by the function template.
return type classname
It is also possible to use nontype arguments that are in additional argument T, we can also use other arguments such as strings, function names, constant expressions and built-in types.
this templates supplies the size of the array as an argument.
You can check latest and updated C++ Job Interview Questions and Answers on this page. You can filter the question set by the company's name. Looking for the right answer? Don't worry! You can click on view answer section and check the answer with the explanation. You can discuss the answer in the forum with other users easily. Login with Facebook and save the answer To Reading List right now!
Our site contains top IT companies placement paper and mock test to help the freshers and experienced candidate to get hands on experience on the pattern and type of questions. We have curated all the C++ technical interview questions and answers from previous candidates.