Friday, July 31, 2009

Wat is virtual function in c++?

C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class. The concept of c++ virtual functions is different from C++ Function overloading.





C++ Virtual Function - Properties:


C++ virtual function is,





A member function of a class


Declared with virtual keyword


Usually has a different functionality in the derived class


A function call is resolved at run-time


The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.














C++ Virtual Function - Reasons:


The most prominent reason why a C++ virtual function will be used is to have a different functionality in the derived class.





For example a Create function in a class Window may have to create a window with white background. But a class called CommandButton derived or inherited from Window, may have to use a gray background and write a caption on the center. The Create function for CommandButton now should have a functionality different from the one at the class called Window.





C++ Virtual function - Example:


This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.





class Window // Base class for C++ virtual function example


{


public:


virtual void Create() // virtual function for C++ virtual function example


{


cout %26lt;%26lt;"Base class Window"%26lt;%26lt;endl;


}


};





class CommandButton : public Window


{


public:


void Create()


{


cout%26lt;%26lt;"Derived class Command Button - Overridden C++ virtual function"%26lt;%26lt;endl;


}


};





void main()


{


Window *x, *y;





x = new Window();


x-%26gt;Create();





y = new CommandButton();


y-%26gt;Create();


}





The output of the above program will be,


Base class Window


Derived class Command Button





If the function had not been declared virtual, then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.





C++ Virtual function - Call Mechanism:


Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.

Wat is virtual function in c++?
A virtual function is a member function of a class that, when redefined by a derived class, behaves in a special way. When you use polymorphism, and point to a derived object using a base class pointer, then call that base class pointer's virtual member function, the program will actually execute the derived class's version of the function.





ex:


class base {


public:


virtual int foo() return 0;


}


class derived : base {


public:


virtual int foo() return 1;


}





int main() {


base* myBasePointer = new derived;


cout%26lt;%26lt;myBasePointer-%26gt;foo();


}





will print out "1" instead of "0"
Reply:C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class. The concept of c++ virtual functions is different from C++ Function overloading.


C++ Virtual Function - Properties:





C++ virtual function is,





* A member function of a class


* Declared with virtual keyword


* Usually has a different functionality in the derived class


* A function call is resolved at run-time





The difference between a non-virtual c++ member function and a virtual member function is, the non-virtual member functions are resolved at compile time. This mechanism is called static binding. Where as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic binding.





C++ Virtual Function - Reasons:





The most prominent reason why a C++ virtual function will be used is to have a different functionality in the derived class.





For example a Create function in a class Window may have to create a window with white background. But a class called CommandButton derived or inherited from Window, may have to use a gray background and write a caption on the center. The Create function for CommandButton now should have a functionality different from the one at the class called Window.


C++ Virtual function - Example:





This article assumes a base class named Window with a virtual member function named Create. The derived class name will be CommandButton, with our over ridden function Create.





class Window // Base class for C++ virtual function example


{


public:


virtual void Create() // virtual function for C++ virtual function example


{


cout %26lt;%26lt;"Base class Window"%26lt;%26lt;endl;


}


};





class CommandButton : public Window


{


public:


void Create()


{


cout%26lt;%26lt;"Derived class Command Button - Overridden C++ virtual function"%26lt;%26lt;endl;


}


};





void main()


{


Window *x, *y;





x = new Window();


x-%26gt;Create();





y = new CommandButton();


y-%26gt;Create();


}





The output of the above program will be,


Base class Window


Derived class Command Button





If the function had not been declared virtual, then the base class function would have been called all the times. Because, the function address would have been statically bound during compile time. But now, as the function is declared virtual it is a candidate for run-time linking and the derived class function is being invoked.


C++ Virtual function - Call Mechanism:





Whenever a program has a C++ virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address. This is how the Dynamic binding happens during a virtual function call.











For more info. make a google search, or log on to


www.http://www.codersource.net/cpp_virtual_f...
Reply:Polymorphism: This is one of the important concepts of Object oriented programming language. The word Polymorphism is made of two words (POLY + MORPHISM) (means one thing have several form.).


Example:


‘+’ mainly used to add two integer values, but if we write its functionality to concatenate two strings, means one thing is doing two different operation.





Types of Polymorphism:


1. Static Polymorphism(Ex. Function Overloading, Operator Overloading etc)


2. Dynamic Polymorphism (virtual function)





Dynamic Polymorphism: (also called as run time polymorphism, late binding,)


Here all the need able information is provided at the run time. This can be achieved using the concept of Virtual function.(base class pointer %26amp; Inheritance)





Virtual function: A function that is defined using virtual keyword. It can be defined as redefining the base class member functions in the derived class.


Lets take an example:





Class base {





Public:


void display()


{


cout%26lt;%26lt;”this is the display function of base class”;





}





};





Class derived : public base {





Public:


Void display()


{





cout%26lt;%26lt;”this is display function of derived class”;





};





Void main()


{


Base *ptr, baseObj;


derived derivedObj;


ptr=%26amp;baseObj; // here we r providing the detail that ptr will call display


function of which class, (detail at runtime.)








ptr-%26gt;display(); // this will display function of base class.





ptr-%26gt;%26amp;derivedObj; // here we r providing the detail that ptr will call display


function of which class( detail at runtime.)





ptr-%26gt;display(); // this will call the display function of derived class.























}








}


No comments:

Post a Comment