Initialise An Array Of Objects With Parameterised Constructors In C++

We need to create things when an array is defined because while defining a class; we need to note that there will not be any storage space allocated. So, using the class creation of objects becomes crucial. There are many ways to Initialise an Array of objects with parameterised constructors in C++; each is discussed below with their respective C++ codes and output.

1. Using Function Calls

C++ code

Example

// 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:

Output

/tmp/2y4t0S9PoQ.o
22
24
26

2. Using the Function malloc

C++ code

Example

// 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:

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

Example

// 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:

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!

Input Required

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