Initialize Array of objects with parameterized constructors in C++
Initialize Array of objects with parameterized constructors in C++
When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to build objects to use the data and access functions specified in the class.
Syntax:
ClassName ObjectName[number of objects];
Specific methods for initializing the Parameterized Constructors list of objects:
Using malloc(): Use malloc() method to avoid calling a non-parameterized constructor. The "Malloc" or "Memory Allocation" method in C++ is used to dynamically allocate the specified size of a single large block of memory. It returns a sort void pointer that can be thrown into a pointer of any kind.
#include <iostream>
#define P 5
using namespace std;
class Test
{
// private variables
int a, b;
public:
// parameterised constructor
Test(int a, int b)
{
this->a = a;
this->b = b;
}
// function to print
void print()
{
cout << a << " " << b << endl;
}
};
int main()
{
// allocating dynamic array
// of Size N using malloc()
Test* arr = (Test*)malloc(sizeof(Test) * P);
// calling constructor
// for each index of array
for (int k = 0; k < P; k++)
{
arr[k] = Test(k, k + 1);
}
// printing contents of array
for (int k = 0; k < P; k++)
{
arr[k].print();
}
return 0;
}
Output:

Using Double pointer: The pointer to a pointer is a set of complex indirect or a pointer chain. A pointer typically holds the address of a variable. When we describe a pointer to a pointer, the first pointer contains the second pointer 's address, which points to the location containing the real value, as shown below. Now we can allocate a number of blocks to be assigned, so we have to call the parameterized constructor to initialize for each index using the new keyword.
#include <iostream>
#define T 10
using namespace std;
class Test {
// private variables
int s, t;
public:
// parameterised constructor
Test(int s, int t)
: s(s), t(t)
{
}
// function to print
void print()
{
cout << s << " " << t << endl;
}
};
int main()
{
// allocating array using
// pointer to pointer concept
Test** arr = new Test*[T];
// calling constructor for each index
// of array using new keyword
for (int i = 0; i < T; i++) {
arr[i] = new Test(i, i + 1);
}
// printing contents of array
for (int i = 0; i < T; i++) {
arr[i]->print();
}
return 0;
}
Output:

Using a new keyword: On the Heap, the new operator denotes a memory allocation request. If there is enough memory, the new operator identifies the memory and returns the newly allocated and configured memory address to the variable name. Here, point-variable is the information-type pointer. Data type could be any type of built-in data along with an array or any type of user-defined data such as structure and class.
If we add a parameterized constructor, a new keyword requires a non-parameterized constructor for dynamic initialization. So we're going to use a dummy constructor for that.
#include <iostream>
#define E 15
using namespace std;
class Test {
// private variables
int m, n;
public:
// dummy constructor
Test() {}
// parameterised constructor
Test(int m, int n)
{
this->m = m;
this->n = n;
}
// function to print
void print()
{
cout << m << " " << n << endl;
}
};
int main()
{
// allocating dynamic array
// of Size N using new keyword
Test* arr = new Test[E];
// calling constructor
// for each index of array
for (int j = 0; j < E; j++) {
arr[j] = Test(j, j + 1);
}
// printing contents of array
for (int j = 0; j < E; j++) {
arr[j].print();
}
return 0;
}
Output:

