Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this blog, we will guide you on creating virtual constructors and virtual destructors, and we will see how to create virtual constructors and destructors. The virtual method works when we have a base class pointer to a derived class object.
The constructor must be non-virtual because when a constructor of a class is executed, there is no virtual table in the memory, which means no virtual pointer defined yet. So, the constructor should never be virtual. But virtual destructor is possible.
Now we will try to write to run the program that contains virtual constructors and destructors and see the output.
Virtual Constructor
The creation of a virtual constructor is not possible because of the reasons given below:
There is no virtual memory table present while calling the constructor. So, the construction of a virtual constructor is not possible.
Because the object is not created, virtual construction is impossible.
The compiler must know the type of object before creating it.
The following program will throw an error because it tries to call a virtual constructor.
#include <iostream>
using namespace std;
class Base
{
public:
virtual Base() // Virtual Constructor
{
cout << "Base created" << endl;
}
void show()
{
cout << "Show the Base class object" << endl;
}
};
class Derived: public Base
{
public:
Derived()
{
cout << "Derived created" << endl;
}
void show()
{
cout << "Show the Derived class object " << endl;
}
};
int main()
{
Base* ptr;
Derived d;
ptr = &d;
ptr->show();
You can also try this code with Online C++ Compiler
constructors cannot be declared 'virtual' [-fpermissive]
Now, lets try to create virtual destructor.
Virtual Destructor
We cannot delete a derived class object using a base class pointer that has a non-virtual destructor. To delete the derived class object using the base class pointer, the base class must contain a virtual destructor. It will be more clear as we understand the following examples.
Example1
In this example, delete b will only call the Base class destructor, but it has not called the Derived class destructor. The object of derived class still exists, which will cause memory leaks.
// CPP program without virtual destructor
#include <iostream>
using namespace std;
class Base {
public:
Base()
{ cout << "Base created\n"; }
~Base()
{ cout<< "Base destroyed\n"; }
};
class Derived: public Base {
public:
Derived()
{ cout << "Derived created\n"; }
~Derived()
{ cout << "Derived destroyed\n"; }
};
int main()
{
Base *b = new Derived;
delete b;
getchar();
return 0;
}
You can also try this code with Online C++ Compiler
In the given example, both Base class and Derived class destructors are called. We are using a virtual destructor for Base Class. This will first call the Derived class destructor, and then the destructor of the Base
class is called.
// CPP program virtual destructor
#include <iostream>
using namespace std;
class Base
{
public:
virtual ~Base()
{
cout << "Base Destroyed\n";
}
};
class Derived:public Base
{
public:
~Derived()
{
cout<< "Derived Destroyed\n";
}
};
int main()
{
Base* b = new Derived; // Upcasting
delete b;
}
You can also try this code with Online C++ Compiler
Is it possible to have a virtual constructor and virtual destructor? We can't have virtual constructors in our program but we can use a virtual destructor. A virtual destructor is used to destroy an object through a base class pointer by calling the derived destructors appropriately.
Why are there no virtual constructors? A constructor can not be virtual because when the constructor of a class is executed, there is no virtual table in the memory, which means no virtual pointer defined yet. Hence the constructor should always be non-virtual.
Why are virtual destructors used? Virtual destructors in C++ are used to avoid memory leaks, especially when your class contains unmanaged code, i.e., pointers or object handles to files, databases, or other external objects.
What is the difference between constructor and destructor? The constructor initializes the object of a class. In contrast, a destructor is used to destroy the instances.
What is the need for a virtual table? The compiler can't know which code will be executed by the o->f() call at the compile time. Since it doesn't know what o points to. Hence, it would be best if we had a "virtual table," which is a table of function pointers.
Key Takeaways
In this article, we extensively discussed virtual constructors and destructors. We have also seen the reasons behind a constructor should be non-virtual. And at the end of the blog, we have seen some examples to properly understand the virtual constructors and destructors.
Check out the link to learn more about C#destructors.
We hope that this blog has helped you enhance your knowledge regarding virtual constructors and destructors and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!