We must instantiate objects upon array declaration in order to indicate that storage space will not be allocated during class definition. Therefore, object instantiation is essential for class creation. Various methods exist for initializing an array of objects with parameterized constructors in C++; each method is detailed below along with its corresponding C++ code and resulting output.
1. Using Function Calls
C++ code
// The below code demonstrates the Concept of How to initialise an Array of
// objects with parameterized constructors in C++
#include <iostream>
using namespace std;
class Test {
// The below variables are private.
private:
private y;
public:
// The below code snippet is a parameter for our concept
Test(int cx, int cy)
{
x = cx;
y = cy;
}
// The below code snippet helps us with creating a
// method to add two numbers for our objects
void add() { cout << x + y << endl; }
};
int main()
{
// the below code helps us with initializiinitialisingects which are
// about three in number with the help of function calls of the
// constructor, which is a parameterised function which has
// the array elements
Test obj[] = { Test(11, 11), Test(12, 12), Test(13, 13) };
// the in-built add method helps us with adding up the elements of the
// array
for (int i = 0; i < 3; i++) {
obj[i].add();
}
return 0;
}
Output:
/tmp/2y4t0S9PoQ.o
22
24
26
2. Using the Function malloc
C++ code
// The below code demonstrates the Concept of How to initialise an Array of
// objects with parameterized constructors in C++
#include <iostream>
#define N 15
using namespace std;
class Test {
// The below variables are private.
int x, y;
public:
// The below code snippet is a parameter for our concept
Test(int x, int y)
{
this->x = x;
this->y = y;
}
// The below function helps us to print the values
void print() { cout << x << " " << y << endl; }
};
int main()
{
// the below code snippet allocates a dynamic array
// which is of size N using the
// pre-defined malloc() function
Test* arr = (Test*)malloc(sizeof(Test) * N);
// the below code snippet calls the constructor in the
// for loop ranging from index 0 to N for each index of the array declared
for (int i = 0; i < N; i++) {
arr[i] = Test(i, i + 1);
}
//The below function helps us to print the content in our array declared
for (int i = 0; i < N; i++) {
arr[i].print();
}
return 0;
}
Output:
/tmp/2y4t0S9PoQ.o
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
3. Using the NEW keyword
C++ code
// The below code demonstrates the Concept of How to initialise an Array of
// objects with parameterized constructors in C++
#include <iostream>
#define N 15
using namespace std;
class Test {
// The below variables are private.
int x, y;
public:
// below, we have created a dummy constructor for testing purposes
Test() {}
// The below code snippet is a parameter for our concept
Test(int x, int y)
{
this->x = x;
this->y = y;
}
// The below function helps us to print the values
void print() { cout << x << " " << y << endl; }
};
int main()
{
// the below code snippet allocates a dynamic array
// which is of size N using the
// using the new keyword
Test* arr = new Test[N];
// the below code snippet calls the constructor in the
// for loop ranging from index 0 to N for each index of the array declared
for (int i = 0; i < N; i++) {
arr[i] = Test(i, i + 1);
}
//The below function helps us to print the content in our array declared
for (int i = 0; i < N; i++) {
arr[i].print();
}
return 0;
}
Output:
/tmp/2y4t0S9PoQ.o
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
Happy Coding!