C++ Virtual Functions

In this article, you are going to learn about an important concept in C++ called the virtual functions and pure virtual functions. However, it is recommended that you learn about basics of pointer, classes and derived classes before reading further.

Advertisements

What is Virtual Function?

 A virtual function is a special type of member function that is declared within a base class and is Overridden by a derived class. When you refer to a derived class object using a pointer or a reference to the base class.

 Its name in the base class and the derived classes remains the same Its definition in these classes is indistinct.

We call a virtual function for that object and execute the derived class version of the function.  The virtual function in the derived class is executed through the pointer of the public base class.

How Virtual Functions are declared?

A virtual function is declared by writing the word virtual before function declaration in the base class. The functions with the same name are declared in the derived classes. The use of the word virtual before function declaration in derived classes is optional.

 Once a function is declared virtual in a base class, it becomes virtual in all derived classes; even when the keyword virtual is not written in its definition in the derived classes.

The different versions of a virtual function have derived classes with it.

 In derived classes, the virtual function is reused. A function that is already used is mentioned to override the base class function.

What is Pure Virtual Function?

 A pure virtual function is a virtual function for which we don’t have a fulfillment, we only declare it.

A pure virtual function is declared by assigning 0 while implementing. The virtual function that is only mentioned but not defined in the base class is called the pure virtual function. 

  • The function is made pure virtual in its previous declaration with the keyword virtual and by adding it with equal to 0.
  • A pure virtual function is usually can be  implemented in a base class and must be implemented in a leaf subclass.

You denote that fact by appending the “= 0” to the declaration:

class Abstract Base
{
       virtual void PureVirtualFunction() = 0;
}

Then you cannot declare and instantiate a subclass without it implementing the pure virtual function:

class Derived: public Abstract Base
{
    virtual void PureVirtualFunction() override { }
}

The pure virtual function tells the compiler that the function is pure. The class that contains the pure virtual function exists only to act as a parent of the derived classes. 

This function is implemented in the derived classes.

The base class that has one or more pure virtual functions is called the Abstract Base Class. These classes are designed as a framework or layout for derived classes. 

An abstract base class cannot be identical, that is an object of its type cannot be defined. A pointer to an abstract base class can be defined. 

The derived class that does not have any pure virtual function is called Concrete Derived Class. The pure virtual function of the abstract. Base Class is used in the Concrete Derived Class. The derived classes have their versions of the virtual functions.

Advertisements

The Abstract Base Class is defined at the top of this hierarchy. The Concrete Derived Classes are derived from the Abstract Base Class. The pure virtual functions are declared in the base class. Each derived class contains its version of these pure virtual functions.

Hence, Inferiority is gained within the hierarchy of the class.

What are the Similarities between virtual and pure virtual function?

  • These are the concepts of run-time polymorphism.
  • The syntax declaration of both the functions remains the same all over the program.
  • These functions cannot be global or static.

Differences between virtual function and pure virtual function?

The virtual function and pure virtual function are concepts of run time polymorphism. The main difference between ‘virtual function’ and ‘pure virtual function’ is that ‘virtual function’ has its definition in the base class and also the inheriting derived classes reuse it. 

The pure virtual function has no definition in the base class, and all the inheriting derived classes have to reuse. However, the virtual function is also called dynamic dispatch and run-time dispatch, due to the function called is specified in the run time following the type of the object used

The virtual function’ has its definition in the base class. A ‘Pure Virtual Function’ has no definition in the base class.

A virtual function is created by declaring the function in the base class and adding the keyword ‘virtual’. 

Virtual functions are defined in the base class and overridden in the derived class. Pure Virtual Function is a base class particularly not defined in the base class.

Example of Virtual Function:

#include<iostream.h>
Class bb
{
   Public:
   Virtual void PPP()
   {
   Cout<<” it is the base class ”<<endl;
}
};

Class d1: public bb
{ 
Public:
Void PPP()
{
Cout<<” first derived class”<<endl;
}
};
Class d2: public bb
{
Public:
Void PPP()
{
Cout<<” second derived class”<<endl;
}
};
Main()
{
bb *p;
d1 a;
d2 b;
p=&a;
p->ppp();
p= &b;
p->ppp();
 }

Output:

first derived class
second derived class   

 Explanation of the above program:

In the above program, the keyword “virtual appears only in the definition of the base class bb: it has not been repeated in the derived classes dl & d2 that are derived from the base class bb. Each derived class d1 and d2 and the base class bb have a member function with the same name. i.e. PPP.

The statement “p = &a;“ assigns the address of object bb to the pointer object “p”. When the statement “p->ppp(); is executed for the first time, the pointer object of the base class contains the address of the object of the derived class d1 and the virtual member function of the derived class dl is executed.

Similarly, when the “p->pppo);” statement is executed for the

the second time, the pointer object ‘p’ of the base class contains the address of the derived class d2 and thus the virtual member function of the derived class d2 is executed

in executing the virtual function in c++, the computer selects the function to be called based on the contents of the pointer and not based on the type of the pointer.

Example of Pure Virtual Function:

#include<iostream>
using namespace std;
class B {
  public:
    virtual void s() = 0; // Pure Virtual Function
};

class D:public B {
  public:
    void s() {
     cout << " Example of Virtual Function in Derived class\n";
    }
};
int main() {
  B *b;
  D dobj; // An object of class D
  b = &dobj;// A pointer of type B* pointing to dobj
  b->s();// prints"D::s() called"
}

Output:

Example of Virtual Function in Derived class.  
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version