Skip to content
Related Articles

Related Articles

Dynamic initialization of object in C++
  • Difficulty Level : Medium
  • Last Updated : 08 Jan, 2021

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:

  1. In the below example, new is used to dynamically initialized 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for dynamic allocation
#include <iostream>
using namespace std;
  
class geeks {
    int* ptr;
  
public:
    // Default constructor
    geeks()
    {
        // Dynamically initializing ptr
        // using new
        ptr = new int;
        *ptr = 10;
    }
  
    // Function to display the value
    // of ptr
    void display()
    {
        cout << *ptr << endl;
    }
};
  
// Driver Code
int main()
{
    geeks obj1;
  
    // Function Call
    obj1.display();
  
    return 0;
}

chevron_right


Output:

10

Dynamic Deallocation:

Approach:

  1. In the below code, delete is used to dynamically free the memory.
  2. The contents of obj1 are overwritten in the object obj2 using assignment operator, then obj1 is deallocated by using delete operator.

Below is the code for dynamic deallocation of the memory using delete operator.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to dynamically
// deallocating the memory
#include <iostream>
using namespace std;
  
class geeks {
    int* ptr;
  
public:
    // Default constructor
    geeks()
    {
        ptr = new int;
        *ptr = 10;
    }
  
    // Function to display the value
    void display()
    {
        cout << "Value: " << *ptr
             << endl;
    }
};
  
// Driver Code
int main()
{
    // Dynamically allocating memory
    // using new operator
    geeks* obj1 = new geeks();
    geeks* obj2 = new geeks();
  
    // Assigning obj1 to obj2
    obj2 = obj1;
  
    // Function Call
    obj1->display();
    obj2->display();
  
    // Dynamically deleting the memory
    // allocated to obj1
    delete obj1;
  
    return 0;
}

chevron_right


Output:

Value: 10
Value: 10

Below C++ program is demonstrating dynamic initialization of objects and calculating bank deposit:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the dynamic
// initialisation as memory is allocated
// to the object
#include <iostream>
using namespace std;
  
class bank {
    int principal;
    int years;
    float interest;
    float returnvalue;
  
public:
    // Default constructor
    bank() {}
  
    // Parameterised constructor to
    // calculate interest(float)
    bank(int p, int y, float i)
    {
        principal = p;
        years = y;
        interest = i;
        returnvalue = principal;
        cout << "\nDeposited amount (float):";
  
        // Finding the interest amount
        for (int i = 0; i < years; i++) {
            returnvalue = returnvalue
                          * (1 + interest);
        }
    }
  
    // Parameterised constructor to
    // calculate interest(integer)
    bank(int p, int y, int i)
    {
        principal = p;
        years = y;
        interest = i;
        returnvalue = principal;
        cout << "\nDeposited amount"
             << " (integer):";
  
        // Find the interest amount
        for (int i = 0; i < years; i++) {
            returnvalue = returnvalue
                          * (1 + interest);
        }
    }
  
    // Display function
    void display(void)
    {
        cout << returnvalue
             << endl;
    }
};
  
// Driver Code
int main()
{
    // Variable initialisation
    int p = 200;
    int y = 2;
    int I = 5;
    float i = 2.25;
  
    // Object is created with
    // float parameters
    bank b1(p, y, i);
  
    // Function Call with object
    // of class
    b1.display();
  
    // Object is created with
    // integer parameters
    bank b2(p, y, I);
  
    // Function Call with object
    // of class
    b2.display();
  
    return 0;
}

chevron_right


Output:

Deposited amount (float):2112.5

Deposited amount (integer):7200

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :