Dynamic Initialization of Object in C++ - GeeksforGeeks
Dynamic Initialization of Object in C++ - GeeksforGeeks
Dynamic Initialization of Object in C++ - GeeksforGeeks
In this article, we will discuss the Dynamic initialization of objects using Dynamic Constructors.
Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial
value of an object is provided during run time.
It can be achieved by using constructors and by passing parameters to the constructors.
This comes in really handy when there are multiple constructors of the same class with different
inputs.
Dynamic Constructor:
The constructor used for allocating the memory at runtime is known as the dynamic
constructor.
The memory is allocated at runtime using a new operator and similarly, memory is deallocated
at runtime using the delete operator.
Dynamic Allocation:
Approach:
https://www.geeksforgeeks.org/dynamic-initialization-of-object-in-c/ 1/6
2/6/24, 10:32 PM Dynamic initialization of object in C++ - GeeksforGeeks
1. In the below example, new is used to dynamically initialize the variable in default
constructor and memory is allocated on the heap.
2. The objects of the class geek calls the function and it displays the value of dynamically
allocated variable i.e ptr.
Below is the program for dynamic initialization of object using new operator:
C++
class geeks {
int* ptr;
public:
// Default constructor
geeks()
{
// Dynamically initializing ptr
// using new
ptr = new int;
*ptr = 10;
}
// Driver Code
int main()
{
geeks obj1;
// Function Call
obj1.display();
return 0;
}
https://www.geeksforgeeks.org/dynamic-initialization-of-object-in-c/ 2/6
2/6/24, 10:32 PM Dynamic initialization of object in C++ - GeeksforGeeks
Output
10
Dynamic Deallocation:
Approach:
Below is the code for dynamic deallocation of the memory using delete operator.
C++
class geeks {
int* ptr;
public:
// Default constructor
geeks()
{
ptr = new int;
*ptr = 10;
}
// Driver Code
int main()
{
// Dynamically allocating memory
// using new operator
geeks* obj1 = new geeks();
https://www.geeksforgeeks.org/dynamic-initialization-of-object-in-c/ 3/6
2/6/24, 10:32 PM Dynamic initialization of object in C++ - GeeksforGeeks
// Function Call
obj1->display();
obj2->display();
return 0;
}
Output
Value: 10
Value: 10
Below C++ program is demonstrating dynamic initialization of objects and calculating bank
deposit:
C++
class bank {
int principal;
int years;
float interest;
float returnvalue;
public:
// Default constructor
bank() {}
// Parameterized constructor to
https://www.geeksforgeeks.org/dynamic-initialization-of-object-in-c/ 4/6
2/6/24, 10:32 PM Dynamic initialization of object in C++ - GeeksforGeeks
// calculate interest(float)
bank(int p, int y, float i)
{
principal = p;
years = y;
interest = i/100;
returnvalue = principal;
cout << "\nDeposited amount (float):";
// Parameterized constructor to
// calculate interest(integer)
bank(int p, int y, int i)
{
principal = p;
years = y;
interest = float(i)/100;
returnvalue = principal;
cout << "\nDeposited amount"
<< " (integer):";
// Display function
void display(void)
{
cout << returnvalue
<< endl;
}
};
// Driver Code
int main()
{
// Variable initialization
int p = 200;
int y = 2;
int I = 5;
float i = 2.25;
return 0;
}
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech
landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at
affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the
millions we've already empowered, and we're here to do the same for you
https://www.geeksforgeeks.org/dynamic-initialization-of-object-in-c/ 6/6