The Virtual
table
l To implement virtual functions, C++ uses a special form of late
binding known as the virtual table. The virtual
table is a lookup table
of functions used to resolve function calls in a dynamic/late binding manner.
The virtual table sometimes goes by other names, such as “vtable”, “virtual
function table”, “virtual method table”, or “dispatch table”.
l Every class that uses virtual functions (or is derived from a
class that uses virtual functions) is given it’s own virtual table.
l This table is simply a static array that the compiler sets up at
compile time.
l A virtual table contains one entry for each virtual function that
can be called by objects of the class. Each entry in this table is simply a
function pointer that points to the most-derived function accessible by that
class.
l The compiler also adds a hidden pointer to the base class, which
we will call *__vptr.
l *__vptr is set (automatically) when a class instance is created so
that it points to the virtual table for that class.
l It makes each class object allocated bigger by the size of one
pointer.
l It also means that *__vptr is inherited by derived classes, which
is important.
l When a class object is created, *__vptr is set to point to the
virtual table for that class. For example, when a object of type Base is
created, *__vptr is set to point to the virtual table for Base.
l When objects of type Derived is constructed, *__vptr is set to
point to the virtual table for Derived
l When these virtual tables are filled out, each entry is filled out
with the most-derived function an object of that class type can call.
Virtual Destructor
l Like constructors, destructors also non inheritable elements of
class.
l Remember, Constructors can not be made virtual, but destructors
can be.
l Although C++ provides a default destructor for your classes if you
do not provide one yourself, it is sometimes the case that you will want to
provide your own destructor (particularly if the class needs to deallocate
memory). You should always make your destructors virtual if
you’re dealing with inheritance.
Example (without virtual
destructor)
#include<iostream.h>
#include<conio.h>
class
Base
{
public:
void fun1()
{ cout<<"\nYou are in class
Base and fun1()"; }
~Base()
{ cout<<"You are in Base
Destructor "; }
};
class
Derived: public Base
{
int a,b;
public:
void fun1() //function overriding
{ cout<<"\nYou are in class
Derived and fun1()"; }
~Derived()
{ cout<<"You are in Derived
Destructor "; }
};
int
main()
{
cout<<"You are in
main()";
{
Base *p=new Derived;
p->fun1();
delete p;
}
getch();
}
Output:
You are in main()
You are in class Base and fun1()
You are in Base Destructor
l You can observe that only Base class destructor executes at the
line delete p;
l To execute Derived class constructor as well as Base class
constructor you need to make destructor virtual.
Example (With Virtual
destructor)
#include<iostream.h>
#include<conio.h>
class
Base
{
public:
void fun1()
{ cout<<"\nYou are in class
Base and fun1()"; }
virtual ~Base()
{ cout<<"You are in Base
Destructor "; }
};
class
Derived: public Base
{
int a,b;
public:
void fun1() //function overriding
{ cout<<"\nYou are in class
Derived and fun1()"; }
~Derived()
{ cout<<"You are in Derived
Destructor "; }
};
int
main()
{
cout<<"You are in
main()";
{
Base *p=new Derived;
p->fun1();
delete p;
}
getch();
}
Output:
You are in main()
You are in class Base and fun1()
You are in Derived Destructor
You are in Base Destructor
Pure Virtual function
A function in a class can be made virtual by writing keyword
virtual and if it has no definition it is called pure virtual function.
Pure virtual function is also known as do-nothing function.
Syntax:
virtual return type function name()=0;
Notice the special way of assigning 0, it is actually not
assignment but just to mention compiler that this function has no body that is
no definition.
If a class is containing pure virtual function:
l Class is known as abstract class, that is we can not create its
object
l To access member of this class we need to define its derived class
since we can not create its object.
l Derived class of abstract class must either redefine pure virtual function
or re-declare it again pure virtual function.
No comments:
Post a Comment