Technical Interview Questions and Answers :: C++
11 / 187
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.
12 / 187
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.
13 / 187
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.
14 / 187
A class created from a class template is called a template class. The syntax for defining an object of a template class is :
Class_name
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.
15 / 187
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:
Syntax:
Template
Class classname
{
----------
---------- ( body of the class)
};
16 / 187
Like class template, we can also define function templates that would be used to create a family of function with different argument types.
Syntax:
Template
return type function name(argument of type T)
{
………..
//body of function
//with type T
//wherever appropriate
}
We must use the template parameter T as and when necessary in the function body and in its arguments list.
17 / 187
Like template class we can use more than one generic data type in the template statement, using comma separator
Syntax:
Template
return type function name(argument of type T1,T2,…)
{
………..
//body of function
}
18 / 187
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.
19 / 187
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.
Syntax:
Template
return type classname
{
……………(function body)
…………….
}
20 / 187
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.
Ex:
Template
Class array
{
T a[size];
……..
……..
};
this templates supplies the size of the array as an argument.