Advanced Placement Computer Science AB:
Lecture 8:  Variables, Constants, and Arithmetic Expressions
    Sewickley Academy, 2000-2001

See Course Home Page.



// lecture8.cpp
//
// These lecture notes are a compilable and executable
// C++ program demonstrating the various topics in the lecture.

#include "stdafx.h"
#include <iostream.h> // for cout

//////////////////////////////////////////////////////
////////////////  Topic 1:  Globals
//////////////////////////////////////////////////////

// 1. Place all your globals at the head of your file, just
//    after your #include statements.
// 2. Use as few globals as possible.
// 3. Preface all global names with "g_"

int g_totalObjects = 3;

//////////////////////////////////////////////////////
////////////////  Topic 2:  Constants
//////////////////////////////////////////////////////

// 1. declare constants by using the "const" keyword
// 2. make constant names ALL CAPS
// 3. don't worry about "g_" for global constants

const bool UNTRUE = 0; // global constant

void constants_demo()
{
 const int MAX_ITEMS = 5; // local constant
 // ...
}

//////////////////////////////////////////////////////
////////////////  Topic 3:  #define and #ifdef
//////////////////////////////////////////////////////

// 1. Use "const", not #define, for constants
//       Why:  Gives better type checking
// 2. Use #define for special case:  code which
//       really should go away altogether for different
//       compilation configurations (eg, debug/release)
// 3. For #ifdef, indicate in comment after #else or #endif
//       the name of the variable (see #endif below)

#define DEBUG

inline void debugPrint(char* functionName, char* message)
{
#ifdef DEBUG
 cout << functionName << ": " << message << endl;
#endif // DEBUG
}

#ifndef DEBUG
blah blah blah // This compiles because DEBUG is defined
#endif // DEBUG

//////////////////////////////////////////////////////
////////////////  Topic 4:  typedef
//////////////////////////////////////////////////////

// 1. Generally, use typedefs sparingly if at all.
// 2. Sometimes useful for compatibility issues

// Say you import some code which expects the non-standard
// type "boolean" to be defined (as the same as the standard
// type "bool".  You can fix this easily by adding the
// following typedef:

typedef bool boolean;

// Now you can use "boolean" as a first-class type:

boolean g_IsHappy;

//////////////////////////////////////////////////////
////////////////  Topic 5:  Scoping
//////////////////////////////////////////////////////

// 1. Globals can be overridden by locals
//    (Not a problem if you use "g_" convention)
// 2. Each inner block can override outer block
// 3. Variable scope is from point of definition to end of block
//    (Bad programming practice -- Don't Do It!)

int w = 10;
int x = 1;
int y = 2;
int z = 3;

void scoping_demo(int y)
{
 x += y; // this is the y in the argument list (5)
 x += z; // this is the global z (3)
 for (int z = 0; z < 2; z++)
 {
  x += z; // this is the local z (0,1,2,3,4)
  x += w; // this is the global w (10), on every iteration!
  int w = 5;
  x += w; // this is the local w (5), on every iteration!
  {
   int w = 6;
   x += w; // this is the most-local w (6)
  }
  x += w; // this again is the local w (5)
 }
 x += z; // this is the local z (2 now)
}

void scoping_demo()
{
 // cout << "Scoping demo" << endl;
 scoping_demo(5);
 // cout << "End of scoping demo" << endl << endl;
}

//////////////////////////////////////////////////////
////////////////  Topic 6:  Static variables
//////////////////////////////////////////////////////

// 1.  Static locals have local scope, but retain
//     their values between invocations
// 2.  Preface all static names with "s_"

void statics_demo1()
{
 static int timesCalled = 0;
 timesCalled++;
 cout << "  timesCalled = " << timesCalled << endl;
}

void statics_demo()
{
 cout << "Statics demo" << endl;
 for (int x = 0; x < 5; x++)
  statics_demo1();
 cout << "End of statics demo" << endl << endl;
}

//////////////////////////////////////////////////////
////////////////  Topic 7:  Uninitialized Variables
//////////////////////////////////////////////////////

// 1. Simple:  initialize variables before using them.  Period.

void uninitialized_variables_demo()
{
 int uninitializedInt;

 // the following will generate a compiler warning
 // (which is desirable, since it is generally a bad idea,
 // and in fact a bug, to use an uninitialized variable).
 cout << "uninitializedInt = " << uninitializedInt << endl;
}

//////////////////////////////////////////////////////
////////////////  Topic 8:  Casting
//////////////////////////////////////////////////////

// 1.  Casting forces type conversion
// 2.  C-style:  (type) expression
//     eg: (int) x;
// 3.  C++-style:  type(expression)
//     eg: int(x);

void casting_demo()
{
 float f;

 f = (1/2) * 3;        // does not work; f <-- 0
 f = (float(1)/2) * 3; // works; f <-- 1.5
 f = (1.0/2) * 3;      // also works
}

//////////////////////////////////////////////////////
////////////////  Topic 9:  Assignment Operators
//////////////////////////////////////////////////////

// 1.  a += b;  // Same as a = a + b;
// 2.  Also works for a -= b; a *= b; a /= b; a %= b;
//     a &= b; a |= b; etc.
// 3.  ++a; // pre-increment a
//     --a; // pre-decrement a
//     Here, the value of a is modified and the modified
//     value is available to the enclosing expression
// 4.  a++; // post-increment a
//     a--; // post-decrement a
//     Here, the enclosing expression gets the premodified
//     value of a, then a is updated

void assignment_operators_demo()
{
 int w = 5;
 int x = 1;
 int y = 2;

 x = y++; // x <- 2 and y <- 3;
 y = --x; // x <- 1 and y <- 1
 y++;     // y <- 2
 x += 3;  // x <- 4
 w *= x++ - --y; // y <- 1, w <- w*(4-1)=15, x <- 5
}

//////////////////////////////////////////////////////
////////////////  Topic 10:  sqrt function
//////////////////////////////////////////////////////

// 1.  Must include <math.h>
// 2.  sqrt(n) takes a double, returns a double

#include <math.h> // normally place at top of file

void sqrt_demo()
{
 double x = 10;
 double y = sqrt(x);
 double z = y*y;
}

//////////////////////////////////////////////////////
////////////////  Topic 11:  argc and argv
//////////////////////////////////////////////////////

// 1. argc = count of command line arguments
//           This includes the application, too.
//           Thus, argc is always at least 1
// 2. argv = array of command line arguments as C strings
//           argv[0] is always the application name
//           (as it was actually invoked)

void argc_argv_demo(int argc, char* argv[])
{
 cout << "argc_argv_demo" << endl;
 cout << "   argc = " << argc << endl;
 for (int x = 0; x < argc; x++)
  cout << "   argv[" << x << "] = " << argv[x] << endl;
 cout << "end of argc_argv_demo" << endl << endl;
}

//////////////////////////////////////////////////////
////////////////  Main routine
//////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
 printf("Lecture 8\n");
 scoping_demo();
 statics_demo();
 uninitialized_variables_demo();
 casting_demo();
 assignment_operators_demo();
 sqrt_demo();
 argc_argv_demo(argc,argv);
 getchar();
 return 0;
}


See Course Home Page.