What is virtual function? Explain with an example - C++

What is virtual function? Explain with an example.

- A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
- Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.
- Consider the following program code :
Class A
{
   int a;
   public:
       A()
       {
          a = 1;
       }
       virtual void show()
       {
          cout <<a;
       }
};

Class B: public A
{
   int b;
   public:
       B()
       {
          b = 2;
       }
       virtual void show()
       {
          cout <<b;
       }
};

int main()
{
   A *pA;
   B oB;
   pA = &oB;
   pA→show();
   return 0;
}
- Output is 2 since pA points to object of B and show() is virtual in base class A.

What are virtual functions?

Polymorphism is also achieved in C++ using virtual functions. If a function with same name exists in base as well as parent class, then the pointer to the base class would call the functions associated only with the base class. However, if the function is made virtual and the base pointer is initialized with the address of the derived class, then the function in the child class would be called.

What is virtual function?

Virtual function is the member function of a class that can be overriden in its derived class. It is declared with virtual keyword. Virtual function call is resolved at run-time (dynamic binding) whereas the non-virtual member functions are resolved at compile time (static binding).

Describe the virtual function and virtual function table.

A virtual function in C++ is :
- A simple member function of a class which is declared with “virtual” keyword
- It usually performs different functionality in its derived classes.
- The resolving of the function call is done at run-time.

Virtual Table :
- A virtual table is a mechanism to perform dynamic polymorphism i.e., run time binging. Virtual table is used to resolve the function calls at runtime. Every class that uses virtual functions is provided with its own virtual functions.
- Every entry in the virtual table is a pointer that points to the derived function that is accessible by that class. A hidden pointer is added by a compiler to the base class which in turn calls *_vptr which is automatically set when an instance of the class is created and it points to the virtual table for that class.
What is a virtual base class? - C+++
What is a virtual base class? - An ambiguity can arise when several paths exist to a class from the same base class....
Explain the use of Vtable.
Explain the use of Vtable - Vtables are used for virtual functions. Its a shortform for Virtual Function Table....
Explain the problem with overriding functions
Explain the problem with overriding functions - Overriding of functions occurs in Inheritance. A derived class may override a base class member function.....
Post your comment
Discussion Board
Easy
It's very easy. I got the concept.
CoerC 11-22-2016
C++ Virtual Functions
Virtual Functions:-
1. Run Time Polymorphic
2. Virtual Keyword required before base function signature.
3. Similar name and signature in derived are required.

By making a function virtual, simply the function calling is get streamlined (Object casting Base = &Child; Base->Virtual). Here, function with virtual keyword before it's signature in base class is being overriden by function with similar name and signature in derived or child class.
know 09-23-2013
C++ Virtual Functions
Virtual Functions:-
1. Run Time Polymorphic
2. Virtual Keyword required in base function signature.
3. Similar signature in derived are required.

By making a function virtual, simply the function calling is get streamlined (Object casting Base = &Child; Base->Virtual). Here, function with virtual keyword before it's signature in base class is being overriden by function with similar name and signature in derived or child class.
know 09-23-2013
it's very easily understand
it's very easily understand
yuvarah 08-26-2013
object Oriented Programming
Nice xamples but i think u all should go towards professional programming which help u a lot in writing program in professional style not in children style gud luck :)
umair 07-20-2013
pure vitual class
a pure virtual class is a virtual class which do not have its function body in base class. its definition can be implemented in their derived class.
syntax:virtual return type function name()=0;
but i have a doubt that virtual function can be declared in base class only but you are declared in derived class also how?
janaki 07-18-2013
C++
nice explanation about virtual functions, but the explanations abt the example is not understandable
raj 07-11-2013
Virtual Function
Excellent

Really helps in my study
Moni 05-28-2013