#include <iostream.h>
class CreateAndDestroy {
public:
CreateAndDestroy( int, char * );
~CreateAndDestroy();
private:
int objectID;
char *message;
};
CreateAndDestroy::CreateAndDestroy(
int objectNumber, char *messagePtr )
{
objectID = objectNumber;
message = messagePtr;
cout << "Object " << objectID << " constructor runs "
<< message << endl;
}
CreateAndDestroy::~CreateAndDestroy()
{
cout << ( objectID == 1 || objectID == 6 ? "\n" : "" );
cout << "Object " << objectID << " destructor runs "
<< message << endl;
}
void create( void );
CreateAndDestroy first(1,"(global before main)");
void main()
{
cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestroy second(2,"(local automatic in main)");
create(); // call function to create objects
cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl;
CreateAndDestroy third(3,"(local automatic in main)");
cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl;
}
void create( void )
{
cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl;
CreateAndDestroy fourth(4,"(local automatic in create)");
cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl;
}
Object 1 constructor runs (global before main)
MAIN FUNCTION: EXECUTION BEGINS
Object 2 constructor runs (local automatic in main)
CREATE FUNCTION: EXECUTION BEGINS
Object 4 constructor runs (local automatic in create)
CREATE FUNCTION: EXECUTION ENDS
Object 4 destructor runs (local automatic in create)
MAIN FUNCTION: EXECUTION RESUMES
Object 3 constructor runs (local automatic in main)
MAIN FUNCTION: EXECUTION ENDS
Object 3 destructor runs (local automatic in main)
Object 2 destructor runs (local automatic in main)
Object 1 destructor runs (global before main)