Complete listing of Variables04.cpp, Listing 4

/*File:  Variables04.cpp
This C++ program illustrates local variables. The
program comments explain the scope of each of the
local variables. 

The program displays the following output on the
screen:

6

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

#include <iostream>
using namespace std;

class Variables04{ 
  public:
  static void classMain(){
    //Instantiate an object in dynamic memory
    // and save its reference in a local pointer
    // variable.  Note that this variable is
    // local to the classMain function.  Its
    // scope extends from the point where it is 
    // declared to the end of the function.  It
    // cannot be accessed from any location
    // outside its scope.
    Variables04* ptrToObject = new Variables04();

    //Use the local pointer variable to invoke an
    // instance function on the object.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables04 class
  void doSomething(){
    //The following statement will not compile
    // because the statement is not within the
    // scope of the variable named localVariable.
    //cout << localVariable << endl;

    //The following variable is local to the
    // method named doSomething.  Its scope
    // extends from the point where it is 
    // declared to the end of the function.
    int localVariable = 6;
    cout << localVariable << endl;
  }//end doSomething function
};//End Variables04 class
//---------------------------------------------//

int main(){
  Variables04::classMain();
  return 0;
}//end main

Listing 4