Listing 7, Expressions01.cpp

/*File:  Expressions01.cpp
This C++ program illustrates expressions and
operators.  In particular, it illustrates the use
of:

The Scope Resolution operator
The Indirection operator
The new operator
The Assignment operator
The Pointer to Member operator
Multiplicative operators
Additive operators
Parenthesis to control precedence
The modulus operator to get a remainder
The cast operator to temporarily change the type
  of a value.
  
The program also illustrates the impact of 
operand type on the result of an arithmetic
operation.

The program displays the following output on the
screen:

Precedence:
  6*3+5 = 23
  (6*3)+5 = 23
  6*(3+5) = 48

Types of arithmetic
  Float:   10.0/3.0 = 3.33333
  Mixed:   10.0/3 = 3.33333
  Integer: 10/3 = 3

The modulus operator
 For 11/3:
  Quotient = 3
  Remainder = 2

The cast operator
  Display as type char: A
  Display as type short: 65
Press any key to continue

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

#include <iostream>
using namespace std;

class Expressions01{ 
  public:
  static void classMain(){
    //Instantiate an object in dynamic memory
    // and save its reference in a pointer 
    // variable.  Note the use of the Indirection
    // operator, the new operator, and the
    // Assignment operator.
    Expressions01* ptrToObject = 
                             new Expressions01();

    //Invoke an instance function on the object.
    // Note the use of the Pointer to Member, or
    // Member access operator.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Expressions01
  // class
  void doSomething(){
    //Illustrate arithmetic operators and the use
    // of parentheses to control precedence.
    cout << "Precedence:\n";
    cout << "  6*3+5 = " << 6*3+5 << endl;
    cout << "  (6*3)+5 = " << (6*3)+5 << endl;
    cout << "  6*(3+5) = " << 6*(3+5) << endl;

    cout << "\nTypes of arithmetic" << endl;
    cout << "  Float:   10.0/3.0 = " 
                             << 10.0/3.0 << endl;
    cout << "  Mixed:   10.0/3 = " << 10.0/3 
                                         << endl;
    cout << "  Integer: 10/3 = " << 10/3 << endl;

    //Illustrate the use of the modulus operator
    // to get a remainder.
    cout << "\nThe modulus operator" << endl;
    cout << " For 11/3:\n";
    cout << "  Quotient = " << 11/3 << endl;
    cout << "  Remainder = " << 11%3 << endl; 

    //Illustrate the use of the cast operator to
    // temporarily change the type of a value.
    cout << "\nThe cast operator" << endl;
    char var = 65;
    cout << "  Display as type char: "<< var 
                                         << endl;
    cout << "  Display as type short: " 
                           << (short)var << endl;
 
  }//end doSomething function
};//End Expressions01 class
//---------------------------------------------//

int main(){
  //Note the use of the Scope Resolution
  // operator.
  Expressions01::classMain();
  return 0;
}//end main

Listing 7