#include <iostream>
using namespace std;

class Object
{
  public:
    ~Object( )
      { cout << "Invoking object destructor" << endl; }
};

void printExit( )
{
    cout << "Invoking the printExit method..." << endl;
}

void foo( )
{
    cout << "Running program" << endl;

    // abort( );      // doesn't call printExit
    // exit( 0 );     // calls printExit, but not destructor for obj in main
    // throw 0;       // doesn't call printExit unless handled in main
}

int main( )
{
    Object obj;

    atexit( printExit );   // will be invoked on normal termination
    foo( );
}