OOPc using C++

With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.

Monday, August 9, 2010

Program 11 : A simple program illustrating Constructor and Destructor

#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