-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

0

1 Answer 1

-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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.