Dynamic Constructor in C++ with Examples Last Updated : 28 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: C++ #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; obj.display(); } Outputgeeks 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: C++ #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]; } Output1 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: C++ #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; } // Deallocate the dynamically allocated memory ~geeks() { delete p; } }; int main() { // default constructor would be called geeks obj1 = geeks(); obj1.display(); // parameterized constructor would be called geeks obj2 = geeks(7); obj2.display(); } Output0 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. Comment More infoAdvertise with us Next Article Dynamic Constructor in C++ with Examples G gyanendra371 Follow Improve Article Tags : C++ cpp-constructor Practice Tags : CPP Similar Reads Importance of Constructors in C++ Constructors are special member functions in C++ that are invoked automatically when an object of a class is created. Their primary role is to initialize objects. In this article, we will learn all the factors that makes the constructor important in C++.Table of ContentInitialization of ObjectsResou 8 min read Copy Constructor in C++ A copy constructor is a type of constructor that creates an object using another object of the same class. The process of initializing members of an object through a copy constructor is known as copy initialization. It is also called member-wise initialization because the copy constructor initialize 6 min read What is conversion constructor in C++? Pre-requisite: Type Conversion in C++ and Use of explicit keyword in C++ A conversion constructor is a single-parameter constructor that is declared without the function specifier explicitly. The compiler uses conversion constructors to convert objects from the type of the first parameter to the typ 3 min read Constructors in C++ In C++, constructors are special methods that are automatically called whenever an object of a class is created. The constructor in C++ has the same name as the class or structure.Example:C++#include <iostream> using namespace std; class A { public: // Constructor of the class without // any p 6 min read Types of Constructors in C++ In C++, there are several types of constructors, each serving a different purpose. Constructors can be classified based on in which situations they are being used. There are 4 types of constructors in C++:Default Constructor: No parameters. They are used to create an object with default values.Param 13 min read Like