C++ 使用参数化构造函数初始化对象数组
我们需要在定义数组时创建对象,因为在定义类时,我们需要注意不会分配任何存储空间。因此,使用类创建对象变得至关重要。在C++中,有多种方法可以使用参数化构造函数初始化对象数组;下面分别讨论了每种方法以及它们相应的C++代码和输出。
1. 使用函数调用
C++代码
// The below code demonstrates the Concept of How to initialise an Array of
// objects with parameterized constructors in C++
#include
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;
}
输出:
/tmp/2y4t0S9PoQ.o
22
24
26
2. 使用函数malloc()
C++代码
// The below code demonstrates the Concept of How to initialise an Array of
// objects with parameterized constructors in C++
#include
#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;
}
输出:
/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. 使用 NEW 关键字
C++ 代码
// The below code demonstrates the Concept of How to initialise an Array of
// objects with parameterized constructors in C++
#include
#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;
}
输出:
/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