CS Electrical And Electronics
@cselectricalandelectronics

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?
CS Electrical And Electronics Staff asked 4 years ago

I need short information.

1 Answers
CS Electrical And Electronics Staff answered 4 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