Write a Cpp program to enter size of array by using constructor and destructor?

All QuestionsCategory: Cpp LanguageWrite a Cpp program to enter size of array by using constructor and destructor?
Chetan Shidling Staff asked 5 years ago

I need short information.

1 Answers
Chetan Shidling Staff answered 5 years ago

Code:

#include <iostream>
using namespace std;
class test
{
int *a;
public:
test(int size)
{
a = new int[size];
cout<<“Constructor msg: Integer array of size : “<<size;
}
~test()
{
delete a;
cout<<“Deconstructor : deleted memory”;
}
};
int main()
{
int s;
cout<<“Enter size:”;
cin>>s;
test T(s);
cout<<endl;
return 0;
}

Output:

Enter size:4
Constructor msg: Integer array of size : 4
Deconstructor : deleted memory