Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

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

Difference between virtual function and pure virtual function

Both virtual and pure virtual functions are the concepts of runtime polymorphism

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 vs pure virtual function

Pure-Virtual-Functions-in-C

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";
}
};

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