Behavior of virtual function in the derived class from the base class and abstract class
Last Updated :
13 Aug, 2024
In this article, we will discuss the behavior of Virtual Function in the derived class and derived class from the Abstract Base Class in C++.
Consider the following program:
C++
// C++ program to illustrate the concept
// of Virtual Function
#include <bits/stdc++.h>
using namespace std;
// Base Class
class Base {
public:
// Virtual Function
virtual void print()
{
cout << "Inside Base" << endl;
}
};
// Derived Class
class Derived : public Base {
};
// Driver Code
int main()
{
// Object of the derived class
Base* b2 = new Derived();
// Function Call to Base Class
b2->print();
return 0;
}
Explanation: When a pointer of the Base Class is declared pointing to an object of the Derived Class, then only declare the function as virtual in the base when it is needed to override it in the derived class. If the function is declared as a virtual in the Base Class, then it is not recommended to override it in the derived class, it will still call the virtual function and execute it.
The virtual keyword only works when going for runtime polymorphism and overriding the function of the base class in the derived class. If a virtual keyword is used, and it is not recommended to override that function in the derived class, then the virtual keyword is of no use. This property doesn't hold true for the Abstract Base class because there is no function body in the Base class as well. Below is the program to illustrate the same:
Program 1:
C++
// C++ program to illustrate the concept
// of virtual function
#include <bits/stdc++.h>
using namespace std;
// Base Class
class Base {
public:
// Virtual Function
virtual void print() = 0;
};
// Derived Class
class Derived : public Base {
// Overriding of virtual function
void print()
{
cout << "Inside Derived"
<< endl;
}
};
// Driver Code
int main()
{
// Object of the derived class
Base* b2 = new Derived();
// Function Call to Base Class
b2->print();
return 0;
}
Program 2:
C++
// C++ program to illustrate the concept
// of virtual function
#include <bits/stdc++.h>
using namespace std;
// Base Class
class Base {
public:
// Virtual Function
virtual void print() = 0;
};
// Derived Class
class Derived : public Base {
};
// Driver Code
int main()
{
// Object of the derived class
Base* b2 = new Derived();
// Function Call to Base Class
b2->print();
return 0;
}
Output:

Explanation: If the above two programs are compared, we observe different behaviors based on whether or not the derived class overrides the pure virtual function print()
from the base class.
In Program 1, the derived class overrides the pure virtual function print()
. This works correctly because the derived class provides an implementation for the pure virtual function. Therefore, when b2->print()
is called, it correctly calls the derived class's implementation.
In Program 2, however, the derived class does not override the pure virtual function print()
. Since the pure virtual function is not implemented in the derived class, the derived class itself remains abstract. Consequently, attempting to instantiate an object of the derived class and calling b2->print()
results in a compilation error because the function is not defined, and the derived class cannot be instantiated.
Similar Reads
How to Call a Virtual Function From a Derived Class in C++? In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir
2 min read
How to Override a Base Class Method in a Derived Class in C++? Function overriding is a concept in object-oriented programming languages, in which a function/method of a parent class is redefined in its child class to change its behavior for the objects of the child class. In this article, we are going to learn how to override a base class function in a derived
2 min read
How to Create a Derived Class from a Base Class in C++? In C++, one of the fundamental concepts of Object Oriented Programming (OOPS) is inheritance, allowing users to create new classes based on existing classes. The class that inherits another class is called derived class and the class that is inherited is called base class. In this article, we will l
2 min read
Base Class Pointer Pointing to Derived Class Object in C++ Prerequisite: Pointers in C++ A pointer is a data type that stores the address of other data types. Pointers can be used for base objects as well as objects of derived classes. A pointer to the object of the derived class and a pointer to the object of the base class are type-compatible (may be used
3 min read
How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To
2 min read