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
Consider following program code:
a = 1;
virtual void show()
Class B: public A
b = 2;
virtual void show()
pA = &oB;
Output is 2 since pA points to object of B and show() is virtual in base class
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
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
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.
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..
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
What is virtual
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................