#include <iostream>
using namespace std;
class Array
{
private:
int *A;
int size;
int length;
public:
Array (int size);
void create();
void display();
~Array();
};
Array::Array(int sz)
{
sz = size;
A = new int [sz];
}
void Array::create()
{
cout<<"Enter number of elements: ";
cin>>length;
cout<<"Enter array elements"<<endl;
for(int i{0}; i<length; i++)
{
cout<<" enter element at "<<i<<":"<<endl;
cin>>A[i];
}
}
void Array::display()
{
for(int i{0}; i<length; i++)
cout<<A[i]<<" ";
cout<<endl;
}
Array::~Array()
{
delete []A;
}
int main()
{
int sz;
cout<<"Enter size of array : ";
cin>>sz;
Array arr(sz);
arr.create();
arr.display();
return 0;
}
i created this class as i was learning array adt, and practicing it. my question is do i have to call the destructor to free the dynamically allocated memory for the array. if yes how to call it in main. this class is made for practicing adt and of beginner level, i was stuck at the point coz we have to delete the memory allocated in heap to prevent the memory leakage, in static storage the destructor is called on its own after the scope finishes of the object.