-3

I have been given a .h file, based on which I need to implement a class that simulates some functions of the array in c++. There are two given instance variables in the .h file

int *array;             /* The dynamic array holding the elements */
int nElements;          /* The number of elements in the array    */

I tried the following as the class constructor (though I know it won't work before I did so):

IntArray::IntArray(int n)
{
array = new IntArray[n];
nElements = n;
}

What should I write to make the constructor work?

This is the original .h file for reference. [1]: https://mega.nz/file/P0FVkBCQ#ME3fK_U7-H8iVHLMC7tyCCKxF-xbCMphEaERunZ-y9c

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23

1 Answers1

-1

The variable array is a pointer to int, so you should allocate an array of int.

IntArray::IntArray(int n)
{
    array = new int[n];
    nElements = n;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70