Hi, I am trying to get my code to run, and I can't figure out the problem.
This program is suppose ask the user to enter and array and then print out the array while using the assignment operator, shallow copy, and copy constructor.
I think the problem is with the SetAll and Print functions definitions.
The problem with SetAll(...) is that you provide the wrong parameter. Why does it require three parameter and what are the meanings?
In other words: Use more descriptive names.
This
1 2
*arr->SetAll(arr, 10)
arr->print();
can impossibly be right.
array while using the assignment operator, shallow copy, and copy constructor
Is this really correct? I would think it is deep copy.
Quite frankly, I don't know what I am doing in the SetAll function. I want to set the array and the array's size from the main function. That's why I thought I needed three parameters.
Is there can specific suggestion as to what I can do to fix the problem?
void PointerDataClass::SetAll( int num,int sizeP, int *ptr)
{
//int x=0;
if(sizeP > 0)
lenp = sizeP;
else
lenp = 10;
p = newint[lenp]; // call destroyP(); before this in order to avoid memory leak
if(sizeP > 0) // Note: copy the values provided
{
for(int i= 0; i< lenp; i++)
{
p[i] = ptr[i];
}
}
else // It seems that it is not necessary to provide data
{
cout << "Please enter 10 numbers. \n";
for(int i= 0; i< lenp; i++)
{
cout << "# "<<i+1 <<": ";
cin >> p[i];
}
}
}
...
int main()
{
PointerDataClass *arr, *arr2;
arr = new PointerDataClass[10];
int num[] = { 1, 2, 3, ... }; // Note: ... -> fill with as many numbers as you want
// Note: sizeof(num) / sizeof(num[0]) -> The number of values of the array
arr[0].SetAll(sizeof(num) / sizeof(num[0]), num); // Note: arr[0]
arr[0].print();
...