Hi,
The idea with a constructor is to send it things that can be used to fill up your private variables. Here is a simple example:
Code:
class Vector
{
public:
Vector(int c, int n)
{
cap = c;
num = n;
}
void show()
{
cout<<cap<<" " <<num<<endl;
}
private:
int cap;
int num;
};
int main()
{
Vector v(10, 20);
v.show();
return 0;
}
I am told this contructor uses an array of elements to initialize the Vector
.
Code:
Vector ( T fill [ ], int size)
{
// don't know what goes here
}
You don't have to be told that the constructor uses an array of elements to initialize the vector, you can see it. The first parameter of the constructor is: T fill []. In C++, brackets mean "an array". You declare an array like this:
int num [] = {10, 20, 30};
or
int num [3] = {10, 20, 30};
When a function has a parameter that is an array, the size of the leftmost dimension does not need to be specified, so for a single dimension array, you can write:
Code:
void someFunc(int anArray[], int size)
{
....
}
The only thing funny about your Vector constructor is the type of the array. Instead of it being a type like 'int', it has a type that is 'T'. That has to do with templates, and it means the Vector constructor can take any type of an array as an argument. The second argument is the size of the array. Notice that the type of the array in the constructor, i.e. type T, matches the type of the pointer that is your private variable:
T * data;
That's a good indication that the array in the constructor is meant to be stored in data. A pointer stores an address, so it looks like data was meant to store the address of the array in the constructor. Or, you can create a new array and assign its address to data and then copy the elements of the constructor array into the new array. Creating a new array and copying the elements into it is probably the way to go.
The way you create a new array is like this:
data = new T[size];
I'll leave the copying of the data from the constructor array to the new array to you.