Demonstrating When Constructors and Destructors Are Called - C++ Class

C++ examples for Class:Destructor

Description

Demonstrating When Constructors and Destructors Are Called

Demo Code

                                                                                                                                         
#include <string> 
#include <iostream> 
using namespace std; 
                                                                                                                                         
class MyClass { /*from  ww w  .  j  a v a2  s  . c o  m*/
public: 
    MyClass( int, string ); // constructor 
    ~MyClass(); // destructor 
private: 
    int objectID; // ID number for object 
    string message; // message describing object 
};
// constructor 
MyClass::MyClass( int ID, string messageString ) 
{ 
    objectID = ID; // set object's ID number 
    message = messageString; // set object's descriptive message 
                                                                                                                                         
    cout << "Object " << objectID << "              constructor runs         " 
        << message << endl; 
}
                                                                                                                                         
// destructor 
MyClass::~MyClass() 
{ 
    // output newline for certain objects; helps readability 
    cout << ( objectID == 1 || objectID == 6 ? "\n" : "" ); 
                                                                                                                                         
    cout << "Object " << objectID << "              destructor runs          " 
        << message << endl; 
}
                                                                                                                                         
void create( void ); // prototype 
                                                                                                                                         
MyClass first( 1, "(global before main)" ); // global object 
                                                                                                                                         
int main() 
{ 
   cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl; 
   MyClass second( 2, "(local automatic in main)" ); 
   static MyClass third( 3, "(local static in main)" ); 
                                                                                                                                         
   create(); // call function to create objects 
                                                                                                                                         
   cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl; 
   MyClass fourth( 4, "(local automatic in main)" ); 
   cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl; 
}
                                                                                                                                         
// function to create objects 
void create( void ) 
{ 
   cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl; 
   MyClass fifth( 5, "(local automatic in create)" ); 
   static MyClass sixth( 6, "(local static in create)" ); 
   MyClass seventh( 7, "(local automatic in create)" ); 
   cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl; 
}

Result


Related Tutorials