#include < iostream >
using namespace std;
// class declaration
class myclass
{
int a;
public:
myclass( ); //constructor
∼myclass( ); //destructor
void show( );
};
myclass::myclass( )
{
cout << "In constructor\n";
a=10;
}
myclass::∼myclass( )
{
cout << "Destructing...\n";
} // ...
void main( )
{
myclass c;
}
OUTPUT
Note:
You will notice that initially you will only get the output
In Constructor
But,After exiting the output screen come back to program and refresh ( Alt+F5 ), Then you will the output
Destructing...
The reason being that the constructor is called when the object is created and destructor is called when the object is destroyed.The object will be destroyed only when we have exited the main program. So we need to refresh after exiting the program and Tada!! You get the output "Destructing..."
No comments:
Post a Comment