C++

C++ Virtual Constructor

C++ doesn’t facilitate us for constructing virtual constructors as it is not possible to override the class’s constructor. So, constructor virtualization is meaningless. There is no concept of creating a virtual constructor in a C++ program, but we may create the virtual destructor there. We can also define it since programming a constructor in C++ can’t be considered virtual because when a class’s constructor is called out, the memory contains no virtual table which indicates that no virtual pointer is created. Thus, the constructor ought to be non-virtual at all times.

However, a virtual destructor could exist. Here, we will show what happens when we create a virtual constructor in C++ as well as the workings of the virtual destructor instead of the virtual constructor.

Example 1:

Let’s create the virtual constructor and begin our code by placing the “iostream” header file. This header file is for the functions that are declared in it like “cin” and “cout”. After this, we add the “std” namespace, so we cannot add this “std” with every function in our code. After this, we create a class which is the base class of our code with the name “my_base” and then add “public” to create the virtual constructor.

The virtual constructor is created here by placing the “virtual” keyword. Inside this virtual constructor, we place a “cout” statement. Below this, we create a function named “show” in which we utilize the “cout” again. After this, we create a derived class of this base class with the name “my_derived” and then establish the “my_derived()” constructor in the “public” field. We insert a “cout” statement into this “my_derived()” constructor. Below it, we construct a function called “show” where we make use of the “cout” once more.

Now, after invoking the “main()”, we create a pointer of the base class with the name “my_ptr” and also create the object of the derived class which is “Obj_d”. After this, we assign the address of the “Obj_d” to the “my_ptr”. Then, we call the “show()” function through “my_ptr”.

Code 1:

#include <iostream>
using namespace std;
class my_base
{
    public:
    virtual my_base()                        
    {
        cout << "Here is my base class" << endl;
    }
    void show()
    {
        cout << "the show function of base class" << endl;
    }
};
class my_derived: public my_base
{
    public:
    my_derived()
    {
        cout << "Here is my derived class" << endl;
    }
    void show()
    {
        cout << "the show function of derived class" <show();
}

Output:
Here, it shows an error message which says that the constructor can’t be declared virtual in the C++ programming. So, we can see that C++ doesn’t allow us to generate the virtual constructor but we may create the virtual destructor.

Example 2:

Let’s solve the previous problem and create the virtual destructor in this code. After declaring the “new_base” class, we place the “public” constructor in which we create the virtual destructor by adding “virtual ~” with the “new_base”. We insert a “cout” statement into this virtual destructor. Below it, we construct a function called “show” that makes use of the “cout”. Next, we make a derived class which is “new_derived” of this “new_base” base class and construct the “new_derived()”destructor in the “public” field. This “new_derived()” destructor now has a “cout” statement added to it.

Below it, we create a function called “show” that uses the “cout” statement again. After calling the “main()” function, we now produce an object of the “obj_d” derived class as well as a pointer of the base class named “ptr1”. Following that, we give the “obj_d” address to the “ptr1”. Next, the “show()” method is invoked using “ptr1”.

Code 2:

#include <iostream>
using namespace std;
class new_base
{
    public:
    virtual ~new_base()                            
    {
        cout << "The base class destructor is here" << endl;
    }
    void show()
    {
        cout << "The show function of the base class" << endl;
    }
};
class new_derived: public new_base
{
    public:
    ~new_derived()
    {
        cout << "The derived class destructor is here" << endl;
    }
    void show()
    {
        cout << "The show function of the base class" <show();
}

Output:
This program uses a pointer object of the “new_base” that points to the “obj_d” derived class. Thus, it calls the “show()” method of the “new_base” class first. Then, it calls the “~new_derived()” method of the “new_derived” class and displays the “~new_base” of the base class.

Example 3:

Here’s another code to generate the “virtual” constructor. After including the “iostream” and the “std” namespace, we generate a class “B”. Below this, we create the “public” constructor which is “B()” and then generate the “cout”. The constructor and destructor functions are defined by utilizing a “public” access specifier in which any object in the class can call.

Now, we also create the “~B()” destructor of this base class in which we utilize the “cout” again. Then, we create the “D” class which is the derived class of the “B” base class and place the “public” here. Inside this “public”, we create the constructor as well as the destructor of the derived class with the names “D()” and “~D”, respectively. Both contain the “cout” inside them. Now, we have the “main()” function. After calling this function, we generate the pointer object of the base class.

Then, we use the “delete” keyword and place “base_ptr” here. In this case, the destructor’s space is deleted by calling the pointer object of the base class.

Code 3:

#include<iostream>
using namespace std;  
class B  
{                              
    public:  
    B()  
{  
    cout<< "Constructor of the Base class" << endl;  
}  
 ~B()  
{  
    cout<< "Destructor of the Base class" << endl;  
}  
};  
 
class D: public B  
{  
    public:
    D()  
{  
    cout << "Constructor of the Derived class" << endl ;  
}  
 ~D()  
{  
    cout << "Destructor of the Derived class" << endl;  
}        
};  
int main()  
{  
    B *base_ptr = new D;
       delete base_ptr;  
}

Output:
The result shows that it uses a pointer object that points to the “B” class in the main function. Thus, it calls the “constructor()” of the “B” class first. Then, it calls the “constructor()” of the “D” class. The pointer object that is held by the destructors of the “B” and “D” classes is then deleted. Without invoking the destructor of the “D” class within the program, the “B” class pointer only eliminates the destructor of the “B” class. As a result, the program’s memory is corrupted.

Conclusion

We discussed the concept of “Virtual construction” in C++ programming. We explored that we can’t create the virtual constructor in C++, but we may create the virtual destructor in our codes. Here, we showed what happened when we create the virtual constructor in C++ programming and the workings of the virtual destructor in our codes. We learned that the constructor can’t be virtual, but we can generate the virtual destructor in our class. We demonstrated some examples and explained these codes thoroughly in this guide.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.