The base type pointer, calls the method but the proper virtual function at runtime is called (dynamically).
Declare a function as virtual in base class, then override it in each derived class, just like redefining, new function must have same signature.
Dynamic binding chooses proper function to call of the virtual address table at runtime, but it only occurs off pointer handles.
A virtual-table is created for every class having one or more virtual functions. The
virtual table is usually implemented as an array of function pointers where each function pointer corresponds to the various implementations of the virtual function by the derived classes.
When an object of this class is created, it contains a virtual-pointer (v-pointer) which points to the start of v-table. Whenever a virtual function is invoked on this object, the runtime system resolves it by following the v-pointer to the v-table to invoke the appropriate function.
Virtual functions are used to extend polymorphic behaviour. VPT:- every class has its own virtual function pointer table. It consist of its virtual functions in an organized way which helps compiler to determine which function to call at run time.
What is pure virtual function? or what is abstract class?
When you define only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class.
You can make a pure virtual function or abstract class this way..
void foo() = 0;
Boo MyBoo; // compilation error
How virtual functions are implemented C++?
What is Virtual Destructor?
Using virtual destructors, you can destroy objects without knowing their type – the correct destructor for the object is invoked using the virtual function mechanism. Note that destructors can also be declared as pure virtual functions for abstract classes.
Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library’s external declarations with a unique namespace that eliminates the potential for those collisions.
A using declaration makes it possible to use a name from a namespace without the scope operator.
Iterator class that is used to traverse through the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators, forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer.
Functor means function pointers. The pointer variable that holds the address of some function.
Syntax for declaring a functor is: return_type (* nameofFunctor)(parameter_type1, 2, 3, ……);
It means nameofFunctor is a functor that can point to functions taking parameter_type1, 2, 3, ….. and returning return_type
Functors are functions with a state. In C++ you can realize them as a class with one or more private members to store the state and with an overloaded operator () to execute the function.