Complete listing of Variables01.cpp, Listing 1

/*File:  Variables01.cpp
This C++ program illustrates global variables.
A global variable is declared and initialized to
a value of 0. This variable is accessed from
three different places in the program where its
current value is displayed.  Then the value is
changed and the value is displayed again. 

The program displays the following on the screen:

In global main function.
globalVariable = 0
globalVariable = 1
In classMain function.
globalVariable = 1
globalVariable = 2
In doSomething function.
globalVariable = 2
globalVariable = 3
Press any key to continue

************************************************/

#include <iostream>
using namespace std;

int globalVariable = 0;//Initialize to 0

class Variables01{ 
  public:
  static void classMain(){
    //Display, modify and then display the value
    // of the global variable again.
    cout << "In classMain function.\n";
    cout << "globalVariable = " << globalVariable
                                         << endl;
    globalVariable = globalVariable + 1;
    cout << "globalVariable = " << globalVariable
                                         << endl;

    //Instantiate an object in dynamic memory
    // and save its reference in a pointer 
    // variable.
    Variables01* ptrToObject = new Variables01();

    //Invoke an instance function on the object.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables01 class
  void doSomething(){
    //Display, modify and then display the value
    // of the global variable again.
    cout << "In doSomething function.\n";
    cout << "globalVariable = " << globalVariable
                                         << endl;
    globalVariable = globalVariable + 1;
    cout << "globalVariable = " << globalVariable
                                         << endl;
  }//end doSomething function
};//End Variables01 class
//---------------------------------------------//

int main(){
  //Display, modify and then display the value
  // of the global variable again.
  cout << "In global main function.\n";
  cout << "globalVariable = " << globalVariable
                                         << endl;
  globalVariable = globalVariable + 1;
  cout << "globalVariable = " << globalVariable
                                         << endl;
  Variables01::classMain();
  return 0;
}//end main

Listing 1