c++ code a destructor to delete the dynamically allocated array!!!
In my code I have to define a class called Array, it simulates a dynamic array of integers and I also have to code a constructor. This all seems to work fine, but I also have to code a destructor that will delete the dynamically allocated array, this is where I run into problems. This is the code that I have.
Code:
____________________________________________________
class Array
{
private:
int size;
int *pointer;
public:
Array()
{
size=0;
pointer=NULL;
}
void load()
{
cout <<"Enter the number of integers in the array.\n\n";
cin >>size;
pointer = new int[size];
int i;
for (i=0; i< size; i++)
{
cout <<"Enter interger # " << i+1 <<"\n";
cin >>*(pointer+i);
}
}//end of load
void print()
{
int i =0;
cout <<"The array contains " <<size << "integers.\n\n";
cout <<"They are:\n";
while (i < size)
{
cout <<i+1<<")\t"<<*(pointer+i)<<endl;
i++;
}//end of loop
}
~Array() //destructor to delete the array
{
delete []pointer;
}
};
void main (void)
{
Array array1;
array1.load();
array1.print();
array1.~Array();
}
__________________________________________________
It runs fine when I comment out the destructor, but with it in my program crashes after it displays the integers in the array. Any help would be great. Thanks