#include <iostream>
using namespace std;
class sample
{
public:
sample()
{
cout<<"Hi ";
}
~sample()
{
cout<<"Bye ";
}
};
int main()
{
sample *obj = new sample();
delete(obj);
return 0;
}
Output
Hi Bye
You can see, "Hi" is written in the constructor (sample()) while "Bye" is written in the destructor (~sample()).
Therefore, when object "obj" is creating, constructor will be called and print "Hi", same as when object is going to be freed using delete(obj), destructor (~sample()) will be called and print "Bye".
Consider the program:
Output
You can see, "Hi" is written in the constructor (sample()) while "Bye" is written in the destructor (~sample()).
Therefore, when object "obj" is creating, constructor will be called and print "Hi", same as when object is going to be freed using delete(obj), destructor (~sample()) will be called and print "Bye".
need an explanation for this answer? contact us directly to get an explanation for this answer