Virtual, Pure Virtual Functions and Abstract Class
Virtual, Pure Virtual Functions and Abstract Class
1
Early Binding
In early binding, the compiler matches the function call with the correct function definition at
compile time.
It is also known as Static Binding or Compile-time Binding.
By default, the compiler goes to the function definition which has been called during compile
time.
You have learned about function overriding in which the base and derived classes have
functions with the same name, parameters and return type. In that case also, early binding takes
place.
So, all the function calls you have studied till now are due to early binding.
2
Example
In function overriding, we called the function with the objects of the classes. Now let's try to
write the same example but this time calling the functions with the pointer to the base class i.e.,
reference to the base class’s object.
class Dogs : public Animals{
#include <iostream> public:
using namespace std; void sound(){
class Animals cout << "Dogs bark" << endl;
{ }
};
public:
void sound() int main(){
{ Animals *a;
cout << "This is parent Dogs d;
class" << endl; a= &d;
} a -> sound(); // early binding
}; return 0;
}
3
Late Binding
In the case of late binding, the compiler matches the function call with the correct function
definition at runtime.
It is also known as Dynamic Binding or Runtime Binding.
In late binding, the compiler identifies the content of object at runtime and then matches the
function call with the correct function definition.
By default, early binding takes place. So if by any means we tell the compiler to perform late
binding, then the problem in the previous example can be solved.
This can be achieved by declaring a virtual function.
4
Virtual function
Virtual Function is a member function of the base class which is overridden in the derived
class. The compiler performs late binding on this function.
To make a function virtual, we write the keyword virtual before the function definition.
If we make any function inside a base class virtual, then that function becomes virtual in all its
derived classes. This means that we don't need to declare that function as virtual separately in
its derived classes.
Do You Know?
We can also call private function of derived class from a base class pointer by declaring that
function in the base class as virtual.
It is because:
Compiler checks if the members of a class are private, public or protected only at compile
time and not at runtime.
Since our function is being called at runtime, so we can call any type of function, private or
public
5
Virtual Table
To accomplish late binding, Compiler creates VTABLEs, for each class with virtual function.
The address of virtual functions is inserted into these tables.
Whenever an object of such class is created the compiler inserts a pointer called vpointer,
pointing to VTABLE for that object.
Hence when function is called, compiler is able to resolve the call by binding the correct
function using the vpointer.
Point to Ponder
The rule is that the compiler selects the function based on the contents of the pointer,
not on the type of the pointer, as in NO VIRTUAL FUNCTION.
6
Virtual Table
7
Pure Virtual Function
Pure virtual function is a virtual function which has no definition.
Pure virtual functions are also called abstract functions.
To create a pure virtual function, we assign a value 0 to the function as follows.
8
Abstract Class
An abstract class has at least one abstract function (pure virtual function).
Abstract classes cannot be instantiated (You cannot make object directly of abstract class).
We can only make objects of its subclass (if they are not abstract).
If we do not override the pure virtual function in derived class, then derived class also
becomes abstract class.
Subclasses of an abstract base class must define the abstract method, otherwise, they will also
become abstract classes.
In an abstract class, we can also have other functions and variables apart from pure virtual
function.