What is virtual function? Explain with an example

          
Interview questions

What is virtual function? Explain with an example.


<< Previous  Next>>

C++  - What is virtual function? Explain with an example - Jan 07, 2009 at 22:10 PM by Anuja Changede

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 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.

C++  - What is virtual function? - August 05, 2008 at 22:10 PM by Amit Satpute.

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.

C++  - What is virtual function? - Feb 23, 2009 at 23:45 PM

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).

C++ Describe the virtual function and virtual function table - April 01, 2009 at 10:30 AM by Vidya Sagar

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..

Also read

What are pure virtual functions?

The base class with pure virtual function can't be instantiated since there is no definition...........

What is a virtual destructor? Explain the use of it.

If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor...............

What is virtual constructors/destructors?

The explicit destroying of object with the use of delete operator to a base class pointer to the object is performed by the destructor of the base-class is invoked on that object................

<<Previous  Next>> 

Write your comment - Share Knowledge and Experience

Discussion Board
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 05:28 AM

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 05:26 AM

it's very easily understand

it's very easily understand

yuvarah 08-26-2013 12:47 AM

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 09:37 AM

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 06:29 AM

C++

nice explanation about virtual functions, but the explanations abt the example is not understandable

raj 07-11-2013 12:14 AM

Virtual Function

Excellent

Really helps in my study

Moni 05-28-2013 03:26 AM

good

it is very useful for studies

sumithvictor 04-18-2013 03:01 AM

good

it is very useful for studies

sumithvictor 04-18-2013 03:00 AM

good

This was very useful for students who r like us.

SHRUTHI.S 10-6-2012 10:57 AM

 

 

 
Latest placement tests
Latest links
 
 
Home | Want a Job? Submit Key Skills | Employer login | My key skills | About us | Sitemap | Contact us