Dynamic Constructor in C++ with Examples

When allocation of memory is done dynamically using dynamic memory allocator new in a constructor, it is known as dynamic constructor. By using this, we can dynamically initialize the objects.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class geeks {
    const char* p;
  
public:
    // default constructor
    geeks()
    {
  
        // allocating memory at run time
        p = new char[6];
        p = "geeks";
    }
  
    void display()
    {
        cout << p << endl;
    }
};
  
int main()
{
    geeks obj = new geeks();
    obj.display();
}

chevron_right


Output:



geeks

Explanation: In this we point data member of type char which is allocated memory dynamically by new operator and when we create dynamic memory within the constructor of class this is known as dynamic constructor.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class geeks {
    int* p;
  
public:
    // default constructor
    geeks()
    {
  
        // allocating memory at run time
        // and initializing
        p = new int[3]{ 1, 2, 3 };
  
        for (int i = 0; i < 3; i++) {
            cout << p[i] << " ";
        }
  
        cout << endl;
    }
};
  
int main()
{
  
    // five objects will be created
    // for each object
    // default constructor would be called
    // and memory will be allocated
    // to array dynamically
    geeks* ptr = new geeks[5];
}

chevron_right


Output:

1 2 3 
1 2 3 
1 2 3 
1 2 3 
1 2 3

dynamically .

Explanation: In this program we have created array of object dynamically. The first object is ptr[0], second is ptr[1] and so on . For each object creation default constructor is called and for each object memory is allocated to pointer type variable by new operator.

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class geeks {
    int* p;
  
public:
    // default constructor
    geeks()
    {
  
        // allocating memory at run time
        p = new int;
        *p = 0;
    }
  
    // parameterized constructor
    geeks(int x)
    {
        p = new int;
        *p = x;
    }
    void display()
    {
        cout << *p << endl;
    }
};
  
int main()
{
  
    // default constructor would be called
    geeks obj1 = geeks();
    obj1.display();
  
    // parameterized constructor would be called
    geeks obj2 = geeks(7);
    obj2.display();
}

chevron_right


Output:

0
7

Explanation: In this integer type pointer variable is declared in class which is assigned memory dynamically when the constructor is called. When we create object obj1, the default constructor is called and memory is assigned dynamically to pointer type variable and initialized with value 0. And similarly when obj2 is created parameterized constructor is called and memory is assigned dynamically.



My Personal Notes arrow_drop_up

A fallen star which will rise again

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.