Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
Virtual Functions

Virtual Functions

Ratings: (0)|Views: 59|Likes:
Published by Naik N.A.
virtual function notes
virtual function notes

More info:

Published by: Naik N.A. on Nov 26, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/12/2010

pdf

text

original

 
Virtual Functions
This document give information about Virtual Functions with examples
.
What is a Virtual Function?
A virtual function is a member function of a class, whose functionality can be over-ridden in itsderived classes. It is declared in the base class using the virtual keyword. The virtual nature isinherited in the subsequent derived classes and the virtual keyword need not be re-stated there.The whole function body can be replaced with a new set of implementation in the derived class.
What is Binding?
Binding refers to the act of associating an object or a class with its member. If we can call amethod fn() on an object o of a class c, we say that the object o is binded with the method fn().This happens at compile time and is known as static or compile - time binding. The calls to thevirtual member functions are resolved during run-time. This mechanism is known as dynamic binding. The most prominent reason why a virtual function will be used is to have a differentfunctionality in the derived class. The difference between a non-virtual member function and avirtual member function is, the non-virtual member functions are resolved at compile time.
How does a Virtual Function work?
Whenever a program has a virtual function declared, a v - table is constructed for the class. Thev-table consists of addresses to the virtual functions for classes that contain one or more virtualfunctions. The object of the class containing the virtual function contains a virtual pointer that points to the base address of the virtual table in memory. Whenever there is a virtual functioncall, the v-table is used to resolve to the function address. An object of the class that contains oneor more virtual functions contains a virtual pointer called the vptr at the very beginning of theobject in the memory. Hence the size of the object in this case increases by the size of the pointer. This vptr contains the base address of the virtual table in memory. Note that virtualtables are class specific, i.e., there is only one virtual table for a class irrespective of the number of virtual functions it contains. This virtual table in turn contains the base addresses of one or more virtual functions of the class. At the time when a virtual function is called on an object, thevptr of that object provides the base address of the virtual table for that class in memory. Thistable is used to resolve the function call as it contains the addresses of all the virtual functions of that class. This is how dynamic binding is resolved during a virtual function call.The following code shows how we can write a virtual function in C++ and then use the same toachieve dynamic or runtime polymorphism.#include <iostream.h>class baseclass{
 
 public:virtual void display(){cout<<”\nBase”;}};class derivedclass : public baseclass{public:void display(){cout<<”\nDerived”;}};void main(){base *ptr = new derivedclass();ptr->display();}In the above example, the pointer is of type base but it points to the derived class object. Themethod display() is virtual in nature. Hence in order to resolve the virtual method call, thecontext of the pointer is considered, i.e., the display method of the derived class is called and notthat of the base. If the method was non virtual in nature, the display() method of the base classwould have been called.
Virtual Constructors and Destructors
A constructor cannot be virtual because at the time when the constructor is invoked the virtualtable would not be available in the memory. Hence we cannot have a virtual constructor.A virtual destructor is one that is declared as virtual in the base class and is used to ensure thatdestructors are called in the proper order. It is to be remembered that destructors are called in thereverse order of inheritance. If a base class pointer points to a derived class object and we sometime later use the delete operator to delete the object, then the derived class destructor is notcalled. Refer to the code that follows:#include <iostream.h>class baseclass{public:~baseclass(){
 
}};class derivedclass : public baseclass{public:~derivedclass(){ }};void main(){base *ptr = new derivedclass();// some codedelete ptr;}In this case the type of the pointer would be considered. Hence as the pointer is of type base, the base class destructor would be called but the derived class destructor would not be called at all.The result is memory leak. In order to avoid this, we have to make the destructor virtual in the base class. This is shown in the example below:#include <iostream.h>class baseclass{ public:virtual ~baseclass(){}};class derived : public base{public:~derivedclass(){}};void main()

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->