Virtual function v/s Pure Virtual function in C++

Difference between virtual function and pure virtual function

Here, in this section we will discuss about the difference between virtual function and pure virtual function in C++. Both virtual and pure virtual functions are the concepts of runtime polymorphism.

Difference between virtual function and pure virtual function in C++

The main difference between virtual function and pure virtual function is that virtual function has its definition in the parent class and the child class which is inheriting it can redefine the definition whereas  a pure virtual function will not have any definition and all the inherited child classes must give a new definition

Let’s discuss the top five differences between virtual function and pure virtual function and establish a clear distinction and purpose of each

Virtual Function

Definition

A virtual function has its definition in the base class

Declaration

virtual funct_name(parameter_list) 
{. . . . . ;
}

Child classes

All the child classes may or may not redefine the virtual function definition of the parent class

Effect

They are hierarchical in nature and do not affect the compilation process if any of the child class do not override the virtual function of a parent class then it’s allowed 

Abstract class

abstract class is not possible

Example program

#include<iostream>
using namespace std;
class B {
public:
  virtual void m()//virtual function  
  {
    cout<<" In Child \n";
  }
};
class D: public B {
  public:
  void m() {
    cout<<"In Child \n";
  }
};
int main() 
{
  D d; // An object of class D
  B *b= &d;// A pointer of type B* pointing to d
  b->m();// prints"D::m() called
}
O/P 
In Child

Pure Virtual Function

Definition

Pure virtual functions will not have any definition in the base class

Declaration


virtual
funct_name(parameter_list)=0;

Child class

All child classes must override the virtual function of the parent class

Effect

If any of the child class failed to override the virtual function of the base class then a compilation error will occur

Abstract class

If a class contains at least one pure virtual function then it is abstract

Example program

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

class D:public B {
public:
void s() {
cout << " Virtual Function in child 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
}

O/P
Virtual Function in child class

Conclusion

Virtual functions and pure virtual functions both have their own importance as virtual function all child classes need not be overridden the virtual function of the parent class and in some situation, whether we want all the child classes must and should redefine the virtual function the parent class then pure virtual function best suits

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription