Dynamic Initialization Using Constructors
Dynamic Initialization Using Constructors
Where constructors play an important role in object creation and can be used to initialize
both static and dynamic data members of a class.
While creating an object, its constructor is called and if the constructor contains logic to
initialize the data members with values, is known as dynamic initialization. This is helpful
because here the value is calculated, retrieved, or determined during runtime, which is
more flexible than static initialization.
Syntax
Open Compiler
#include <iostream>
class Rectangle {
public:
void display() {
cout << "Width: " << width << ", Height: " << height << endl;
}
};
int main() {
rect->display();
// Deallocate memory
delete rect;
return 0;
Output
Explanation
The delete rect; deallocates the memory used by the Rectangle object.
Using a constructor to initialize dynamically within C++ makes it so much easier to create
an object where the values get determined only at runtime. Encapsulation of initialization
logic within the constructor makes the code clean, efficient, and more maintainable; use it
whenever object initialization depends upon runtime data.
Dynamic constructors in C++ are constructors that are dynamically allocated on the heap
using the new operator. This allows you to create objects at runtime, rather than at
compile time.
Memory Management: You have more control over memory allocation and
deallocation, which can be crucial for performance-critical applications.
To create a dynamic object, you use the new operator followed by the class name. This
allocates memory on the heap for the object and returns a pointer to it.
Example:
#include <iostream>
class MyClass {
public:
int data;
MyClass(int value) {
data = value;
}
};
int main() {
delete obj;
return 0;
Output:
Data: 10
When you're finished with a dynamic object, you must delete it using the delete operator
to free the memory allocated for it. Failure to do so can lead to memory leaks.
You can also create arrays of dynamic objects using the new operator.
Example:
#include <iostream>
class MyClass {
public:
int data;
MyClass(int value) {
data = value;
};
int main() {
arr[i].data = i + 1;
delete[] arr;
return 0;
Output:
Data: 1
Data: 2
Data: 3
Data: 4
Data: 5
Key Points
You must delete dynamic objects using the delete operator to avoid memory leaks.
You can create arrays of dynamic objects using the new operator.
The constructor which allocates a block of memory that can be accessed by the objects at
run time is known as Dynamic Constructor.
In simple terms, a Dynamic constructor is used to dynamically initialize the objects, that is
memory is allocated at run time.
To define a dynamic constructor in C++, the new keyword is used. New is a keyword in C++
that is used for the dynamic allocation of memory and is used for dynamically initializing
objects.
Let us understand Dynamic constructors in C++ with the help of an example that consists
of class A.
We will dynamically allocate memory to the data members inside the default constructor
of class A as well as a parameterized constructor of class A and check the output.
#include <iostream>
class A
int *value;
public:
A()//Default constructor
*value = 1729;
*value= p_value+1;
void display()
~A()
delete value ;
};
int main()
A obj1, obj2(7225);
return 0;
Output:
Explanation:
We created a dynamic memory within the constructor of a class by using the New
keyword.
This represents a Dynamic constructor in both the default as well as parameterized
constructor.
Here, the memory allocation is done at run time, that is the values are assigned as well as
the constructor is invoked at run time when the object is created.
#include<iostream>
class Blogs
public:
// default constructor
Blogs()
void display()
};
int main()
Blogs obj1;
obj1.display();
Output:
Explanation:
When we initialize the variable banner using New keyword, it invokes a dynamic
constructor thus allocating space in the memory at runtime.
#include <iostream>
class scaler {
int* views_cnt;
public:
// default constructor
scaler()
// and initializing
};
int main()
Output:
12345
12345
12345
12345
12345
Explanation:
We created an array of 5 objects that calls dynamic constructors individually and memory
is allocated for each object at run time.
Conclusion