Initialise An Array Of Objects With Parameterised Constructors In C++ - C++ Programming Tutorial
C++ Course / Dynamic Programming / Initialise An Array Of Objects With Parameterised Constructors In C++

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

BLUF: Mastering Initialise An Array Of Objects With Parameterised Constructors 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: Initialise An Array Of Objects With Parameterised Constructors In C++

C++ is renowned for its efficiency. Learn how Initialise An Array Of Objects With Parameterised Constructors In C++ enables low-level control and high-performance computing in the tutorial below.

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

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:

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