Dynamic Initialization Of Objects In C++ - C++ Programming Tutorial
C++ Course / Object-Oriented Programming / Dynamic Initialization Of Objects In C++

Dynamic Initialization Of Objects In C++

BLUF: Mastering Dynamic Initialization Of Objects In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Dynamic Initialization Of Objects In C++

C++ is renowned for its efficiency. Learn how Dynamic Initialization Of Objects In C++ enables low-level control and high-performance computing in the tutorial below.

In this scenario, we will discuss the utilization of dynamic constructors for initializing objects.

"Dynamic initialization of an object" signifies the process of setting up an object at runtime or assigning its starting value. This can be achieved by employing constructors and supplying them with parameters. This technique is particularly valuable when a class contains multiple constructors, each accepting a distinct set of inputs.

Dynamic Constructor:

The phrase "dynamic function Object { [native code] }" denotes the Object function that dynamically allocates memory at runtime.

The fresh operator is utilized for allocating memory during program execution, while the erase operator is employed for releasing memory during program execution.

Dynamic Allocation:

Approach:

In the instance provided, memory is reserved in the heap and a variable's default constructor Object { [native code] } is dynamically instantiated using the new keyword.

The function is invoked by instances of the "jtp" class, which subsequently displays the value of the dynamically allocated variable ptr.

The program for dynamic object creation through the new keyword is demonstrated 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 free up memory in the code snippet provided, utilize the delete keyword. The assignment operator is employed to replace the data within object obj1 with that of object obj2, while the delete operator is utilized to release the memory allocated for object obj1.

The code snippet below demonstrates how to use the delete operator to dynamically release memory.

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 following C++ code demonstrates the dynamic initialization of objects and the computation of 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 instance, we are presenting a "Student" class, which contains a pair of private attributes.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience