C++'s Vocabulary

Tom Kelliher, CS17

Feb. 16, 1996

The Character Set and Tokens

Valid characters

Characters which can be used in C++ programs ( character set):

  1. Numeric digits: 0--9
  2. Lowercase letters: a--z
  3. Uppercase letters: A--Z
  4. Space character (ASCII 32)
  5. Special characters (see pg. 78)

Case sensitivity: birthyear vs. birthYear

Tokens

A language element that can be used in forming higher-level language constructs.

A sequence of characters treated as one ``symbol''

Token types:

  1. Reserved words
  2. Identifiers
  3. Constants
  4. String literals
  5. Punctuators
  6. Operators

Reserved Words

``Pre-Declared,'' unchangeable meaning

Examples: if, else, while, do signed int float

Identifiers

User-Defined words

Represent:

  1. Variables: entities which can take on different values
  2. Functions: entities which solve problem, sub-problems
  3. Types: (disregard for now)

Identifiers should make plain what they represent:

Poor names: x y z

Minimal rules for constructing an identifier:

  1. No reserved words
  2. First character must be from _, upper-, lower-case letters
  3. Subsequent characters may also be digits

Examples:

Constants

  1. Numeric: 345, 063, 0x4FE, 3.14159, -6e-3
  2. Character constants: 'a', '+', '\n', '\0', '\n', '2', '\'', '\\ '
  3. String literals (constants): "Hi, I'm a string!\n", "embedded quote:\", oh my!"

"Z" vs. 'Z'

Punctuators

Separate syntactical units.

Example:

tax = grossPay * 0.28;
netPay = grossPay - tax;

Operators

Operate on Operands

Types:

  1. Unary: !, -
  2. Binary: -, *
  3. Ternary: ? :

An Example Program and Its Parts

/**********************************************************************
 * Tom Kelliher
 * Feb. 16, 1996
 * 
 * This is just a short demonstration program.  It computes the area
 * of circles.
 **********************************************************************/

#include <iostream.h>   // library file

const float PI = 3.14159;   // a symbolic constant

float area(float radius);   // function declaration

int main()
{

   float radius;

   cout << "Enter a radius of 0.0 to exit" << endl;

   cout << "Radius: ";
   cin >> radius;

   while (radius > 0.0)
   {
      cout << "The area is " << area(radius) << endl << endl;
      cout << "Radius: ";
      cin >> radius;
   }

   return 0;
}


/**********************************************************************
 * area --- compute the area of a circle
 * 
 * input: radius of the circle
 * return value: area of the circle
 **********************************************************************/

float area(float radius)   // function definition
{
   return PI * radius * radius;
}



Thomas P. Kelliher
Thu Feb 15 12:15:37 EST 1996
Tom Kelliher