25-08-2017, 09:32 PM
C++ Interview Questions
C++ Interview.pdf (Size: 61.52 KB / Downloads: 140)
1. What is virtual constructors/destructors?
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying
the delete operator to a base-class pointer to the object, the base-class destructor function
(matching the pointer type) is called on the object.
There is a simple solution to this problem – declare a virtual base-class destructor. This makes all
derived-class destructors virtual even though they don’t have the same name as the base-class
destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator
to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.
2. What do you mean by pure virtual functions?
A pure virtual member function is a member function that the base class forces derived classes to
provide. Normally these member functions have no implementation. Pure virtual functions are
equated to zero.
3.What is namespace?
Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say
it somehow, they serve to split the global scope in sub-scopes known
as namespaces.
4. What is RTTI?
Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a
pointer or a reference to the base type. RTTI is the official way in
standard C++ to discover the type of an object and to convert the type of a pointer or reference
(that is, dynamic typing). The need came from practical experience with
C++. RTTI replaces many homegrown versions with a solid, consistent approach.
5. What is a template?
Templates allow to create generic functions that admit any data type as parameters and return value
without having to overload the function with all the possible data types. Until certain point they fulfill
the functionality of a macro.
6. What do you mean by inline function?
The idea behind inline functions is to insert the code of a called function at the point where the
function is called. If done carefully, this can improve the application's
performance in exchange for increased compile time and possibly (but not always) an increase in the
size of the generated binary executables.
7. What is virtual class and friend class?
Friend classes are used when two or more classes are designed to work together and need access
to each other's implementation in ways that the rest of the world
shouldn't be allowed to have. In other words, they help keep private things private. For instance, it
may be desirable for class DatabaseCursor to have more privilege to the internals of class Database
than main() has.
8. Difference between realloc() and free()?
The free subroutine frees a block of memory previously allocated by the malloc subroutine.
Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a
null value, no action will occur. The realloc subroutine changes the size of the block of memory
pointed to by the Pointer parameter to the number of bytes specified by the Size parameter and
returns a new pointer to the block. The pointer specified by the Pointer parameter must have been
created with the malloc, calloc, or realloc subroutines and not been deallocated with the free or
realloc subroutines. Undefined results occur if the Pointer parameter is not a valid pointer.