Dynamic Initialization Of Objects In C++

In this context, we'll talk about using dynamic constructors to initialise objects.

The term "dynamic initialization of an object" refers to initialising an object during runtime, or giving it its initial value then. It can be done by utilizing constructors and providing them with parameters . It is quite useful when a class has many constructors, each with a different set of inputs.

Dynamic Constructor:

The term "dynamic function Object { [native code] }" refers to the function Object { [native code] } that is used to allocate memory during runtime.

The new operator is used to allocate memory at runtime, whereas the delete operator is used to deallocate memory at runtime.

Dynamic Allocation:

Approach:

In the example below, memory is allocated on the heap and a variable's default function Object { [native code] } is dynamically initialised using new.

The function is called by objects of the class "jtp" which then outputs the value of the ptr dynamically allocated variable.

The software for dynamic object initialization using the new operator is shown below:

Example

#include <iostream>
using namespace std;

class jtp {
int* ptr;

public:
jtp()
{
ptr = new int;
*ptr = 10;
}

void display()
{
cout<< *ptr<<endl;
}
};

int main()
{
jtp obj1;

obj1.display();

return 0;
}

Output:

Dynamic Deallocation:

Approach:

To dynamically release the memory in the code below, use delete. Assignment operator is used to overwrite the contents of object obj1 in object obj2 , and delete operator is used to deallocate object obj1.

The code for utilising the delete operator to dynamically deallocate memory is provided below.

Example

#include <iostream>
using namespace std;

class jtp {
int* ptr;

public:
jtp()
{
ptr = new int;
*ptr = 10;
}

void display()
{
cout<< "Value: " << *ptr
<<endl;
}
};

int main()
{
jtp* obj1 = new jtp();
jtp* obj2 = new jtp();

obj2 = obj1;

obj1->display();
obj2->display();

delete obj1;

return 0;
}

Output:

Output

Value: 10
Value: 10

The C++ program below shows how to dynamically initialise objects and calculate bank deposits:

Example

#include <iostream>
using namespace std;

class bank {
int principal;
int years;
float interest;
float returnvalue;

public:
bank() {}

bank(int p, int y, float i)
{
principal = p;
years = y;
interest = i/100;
returnvalue = principal;
cout<< "\nDeposited amount (float):";

for (int i = 0; i < years; i++) {
returnvalue = returnvalue
* (1 + interest);
}
}

bank(int p, int y, int i)
{
principal = p;
years = y;
interest = float(i)/100;
returnvalue = principal;
cout<< "\nDeposited amount"
<< " (integer):";

for (int i = 0; i < years; i++) {
returnvalue = returnvalue
* (1 + interest);
}
}

void display(void)
{
cout<<returnvalue
<<endl;
}
};

int main()
{
int p = 300;
int y = 3;
int I = 6;
float i = 3.25;

bank b1(p, y, i);

b1.display();

bank b2(p, y, I);

b2.display();

return 0;
}

Output:

Output

Deposited amount (float):330.211

Deposited amount (integer):357.305

Example:

In this example, we have a "Student" class here, and it has two private data members.

1) rNo - to keep track of the roll number

2) use a perc function to save a percentage.

Example

#include <iostream>
using namespace std;

struct Student {
private:
int rNo;
float perc;

public:
Student(int r, float p)
{
rNo = r;
perc = p;
}

void read(void)
{
cout<< "Enter roll number: ";
cin>>rNo;
cout<< "Enter percentage: ";
cin>>perc;
}
void print(void)
{
cout<<endl;
cout<< "Roll number: " <<rNo<<endl;
cout<< "Percentage: " <<perc<< "%" <<endl;
}
};

int main()
{
cout<< "Enter roll number to initialize the object: ";
int roll_number;
cin>>roll_number;
cout<< "Enter percentage to initialize the object: ";
float percentage;
cin>> percentage;

struct Student std(roll_number, percentage);
cout<< "After initializing the object, the values are..." <<endl;
std.print();

std.read();
std.print();

return 0;
}

Output:

Output

Enter roll number to initialize the object: 20
Enter percentage to initialize the object: 40
After initializing the object, the values are...

Roll number: 20
Percentage: 40%
Enter roll number: 30
Enter percentage: 50
Roll number: 30
Percentage: 50%

Input Required

This code uses input(). Please provide values below: