Complete listing of Variables03.cpp, Listing 3

/*File:  Variables03.cpp
This C++ program illustrates instance variables.

This program instantiates two objects of type 
Variables03 in dynamic memory and stores pointers
to the two objects in pointer variables named 
ptrToObj01 and ptrToObj02.

The class named Variables03 has a public instance
variable named instanceVariable.

The program uses the two pointer variables to 
store values of 3 and 10 in the instance variable
belonging to each of the two objects 
respectively.

Then the program invokes an instance function 
named displayInstanceVariable on each of the two 
objects to display the contents of the instance 
variable encapsulated in each of the two objects.

The program displays the following output on the
screen:

In displayInstanceVariable function
3
In displayInstanceVariable function
10

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

#include <iostream>
using namespace std;

class Variables03{
  public:
  //The following is an instance variable.
  int instanceVariable;

  static void classMain(){
    //Instantiate two objects in dynamic memory
    // and save their references in two pointer 
    // variables.
    Variables03* ptrToObj01 = new Variables03();
    Variables03* ptrToObj02 = new Variables03();

    //Store different numeric values in the
    // instance variables belonging to each of
    // the two objects.
    ptrToObj01 -> instanceVariable = 3;
    ptrToObj02 -> instanceVariable = 10;

    //Invoke the same instance function on each
    // of the two objects to display the values
    // contained in the instance variable in
    // each of the two objects.
    ptrToObj01 -> displayInstanceVariable();
    ptrToObj02 -> displayInstanceVariable();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Variables03 class
  void displayInstanceVariable(){
    cout << 
         "In displayInstanceVariable function\n";
    cout << instanceVariable << endl;
  }//end displayInstanceVariable function
};//End Variables03 class
//---------------------------------------------//

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

Listing 3