asm
Syntax:
  asm( "instruction" );

The asm command allows you to insert assembly language commands directly into your code. Various different compilers allow differing forms for this command, such as

   asm {
     instruction-sequence
   }            

or

   asm( instruction );          

auto

The keyword auto is used to declare local variables, and is purely optional.


bool

The keyword bool is used to declare Boolean logic variables; that is, variables which can be either true or false.

For example, the following code declares a boolean variable called done, initializes it to false, and then loops until that variable is set to true.

   bool done = false;
   while( !done ) {
   ...
   }            

Also see the data types page.


break

The break keyword is used to break out of a do, for, or while loop. It is also used to finish each clause of a switch statement, keeping the program from "falling through" to the next case in the code. An example:

   while( x < 100 ) {
     if( x < 0 )
       break;
     cout << x << endl;
     x++;
   }            

A given break statement will break out of only the closest loop, no further. If you have a triply-nested for loop, for example, you might want to include extra logic or a goto statement to break out of the loop.


case

The case keyword is used to test a variable against a certain value in a switch statement.


catch

The catch statement handles exceptions generated by the throw statement.


char

The char keyword is used to declare character variables. For more information about variable types, see the data types page.


class
Syntax:
  class class-name : inheritance-list {
  private-members-list;    
  protected:
  protected-members-list;
  public:
  public-members-list;
  } object-list;

The class keyword allows you to create new classes. class-name is the name of the class that you wish to create, and inheritance-list is an optional list of classes inherited by the new class. Members of the class are private by default, unless listed under either the protected or public labels. object-list can be used to immediately instantiate one or more instances of the class, and is also optional. For example:

   class Date {
     int Day;
     int Month;
     int Year;
   public:
     void display();
   };           

const

The const keyword can be used to tell the compiler that a certain variable should not be modified once it has been initialized.

It can also be used to declare functions of a class that do not alter any class data.


const_cast
Syntax:
  const_cast<type> (object);

The const_cast keyword can be used to remove the const or volatile property from some variable. The target data type must be the same as the source type, except (of course) that the target type doesn't have to be const.


continue

The continue statement can be used to bypass iterations of a given loop.

For example, the following code will display all of the numbers between 0 and 20 except 10:

   for( int i = 0; i < 21; i++ ) {
     if( i == 10 ) {
       continue;
     }
     cout << i << " ";
   }            

default

A default case in the switch statement.


delete
Syntax:
  delete p;
  delete[] pArray;

The delete operator frees the memory pointed to by p. The argument should have been previously allocated by a call to new. The second form of delete should be used to delete an array.


do
Syntax:
  do {
  statement-list;
  } while( condition );

The do construct evaluates the given statement-list repeatedly, until condition becomes false. Note that every do loop will evaluate its statement list at least once, because the terminating condition is tested at the end of the loop.


double

The double keyword is used to declare double precision floating-point variables. Also see the data types page.


dynamic_cast
Syntax:
  dynamic_cast<type> (object);

The dynamic_cast keyword casts a datum from one type to another, performing a runtime check to ensure the validity of the cast. If you attempt to cast between incompatible types, the result of the cast will be NULL.


else

The else keyword is used as an alternative case for the if statement.


enum
Syntax:
  enum name {name-list} var-list;

The enum keyword is used to create an enumerated type named name that consists of the elements in name-list. The var-list argument is optional, and can be used to create instances of the type along with the declaration. For example, the following code creates an enumerated type for colors:

In the above example, the effect of the enumeration is to introduce several new constants named red, orange, yellow, etc. By default, these constants are assigned consecutive integer values starting at zero. You can change the values of those constants, as shown by the next example:

When executed, the above code will display the following output:

Note that the above examples will only work with C++ compilers. If you're working in regular C, you will need to specify the enum keyword whenever you create an instance of an enumerated type:


explicit

When a constructor is specified as explicit, no automatic conversion will be used with that constructor -- but parameters passed to the constructor may still be converted. For example:

  struct foo {
    explicit foo( int a )
      : a_( a )
    { }

    int a_;
  };

  int bar( const foo & f ) {
    return f.a_; 
  }

  bar( 1 );  // fails because an implicit conversion from int to foo
             // is forbidden by explicit.

  bar( foo( 1 ) );  // works -- explicit call to explicit constructor.

  bar( foo( 1.0 ) );  // works -- explicit call to explicit constructor
                      // with automatic conversion from float to int.

export

The export keyword is intended to allow definitions of C++ templates to be separated from their declarations. While officially part of the C++ standard, the export keyword is only supported by a few compilers (such as the Comeau C++ compiler) and is not supported by such mainstream compilers as GCC and Visual C++.


extern

The extern keyword is used to inform the compiler about variables declared outside of the current scope. Variables described by extern statements will not have any space allocated for them, as they should be properly defined elsewhere.

Extern statements are frequently used to allow data to span the scope of multiple files.


false

The Boolean value of "false".


float

The float keyword is used to declare floating-point variables. Also see the data types page.


for
Syntax:
  for( initialization; test-condition; increment ) {
  statement-list;
  }

The for construct is a general looping mechanism consisting of 4 parts:

  1. the initialization, which consists of 0 or more comma-delimited variable initialization statements
  2. the test-condition, which is evaluated to determine if the execution of the for loop will continue
  3. the increment, which consists of 0 or more comma-delimited statements that increment variables
  4. and the statement-list, which consists of 0 or more statements that will be executed each time the loop is executed.

For example:

   for( int i = 0; i < 10; i++ ) {
     cout << "i is " << i << endl;
   }
   int j, k;
   for( j = 0, k = 10;
        j < k;
        j++, k-- ) {
     cout << "j is " << j << " and k is " << k << endl;
   }
   for( ; ; ) {
     // loop forever!
   }            

friend

The friend keyword allows classes or functions not normally associated with a given class to have access to the private data of that class.


goto
Syntax:
  goto labelA;
  ...
  labelA:

The goto statement causes the current thread of execution to jump to the specified label. While the use of the goto statement is generally considered harmful, it can occasionally be useful. For example, it may be cleaner to use a goto to break out of a deeply-nested for loop, compared to the space and time that extra break logic would consume.


if
Syntax:
  if( conditionA ) {
    statement-listA;
  }
  else if( conditionB ) {
    statement-listB;
  }
  ...
  else {
    statement-listN;
  }

The if construct is a branching mechanism that allows different code to execute under different conditions. The conditions are evaluated in order, and the statement-list of the first condition to evaluate to true is executed. If no conditions evaluate to true and an else statement is present, then the statement list within the else block will be executed. All of the else blocks are optional.


inline
Syntax:
  inline int functionA( int i ) {
  ...
  }

The inline keyword requests that the compiler expand a given function in place, as opposed to inserting a call to that function. The inline keyword is a request, not a command, and the compiler is free to ignore it for whatever reason.

When a function declaration is included in a class definition, the compiler should try to automatically inline that function. No inline keyword is necessary in this case.


int

The int keyword is used to declare integer variables. Also see the data types page.


long

The long keyword is a data type modifier that is used to declare long integer variables. For more information on long, see the data types page.


mutable

The mutable keyword overrides any enclosing const statement. A mutable member of a const object can be modified.


namespace
Syntax:
  namespace name {
  declaration-list;
  }

The namespace keyword allows you to create a new scope. The name is optional, and can be omitted to create an unnamed namespace. Once you create a namespace, you'll have to refer to it explicitly or use the using keyword.


new
Syntax:
  pointer = new type;
  pointer = new type( initializer );
  pointer = new type[size];
  pointer = new( arg-list ) type...

The new operator (valid only in C++) allocates a new chunk of memory to hold a variable of type type and returns a pointer to that memory. An optional initializer can be used to initialize the memory. Allocating arrays can be accomplished by providing a size parameter in brackets.

The optional arg-list parameter can be used with any of the other formats to pass a variable number of arguments to an overloaded version of new(). For example, the following code shows how the new() function can be overloaded for a class and then passed arbitrary arguments:

  class Base {
  public:
    Base() { }

    void *operator new( unsigned int size, string str ) {
      cout << "Logging an allocation of " << size << " bytes for new object '" << str << "'" << endl;
      return malloc( size );
    }

    int var;
    double var2;
  };

  ...

  Base* b = new ("Base instance 1") Base;

If an int is 4 bytes and a double is 8 bytes, the above code generates the following output when run:

  Logging an allocation of 12 bytes for new object 'Base instance 1'

operator
Syntax:
  return-type class-name::operator#(parameter-list) {
  ...
  }
  return-type operator#(parameter-list) {
  ...
  }

The operator keyword is used to overload operators. The sharp sign (#) listed above in the syntax description represents the operator which will be overloaded. If part of a class, the class-name should be specified. For unary operators, parameter-list should be empty, and for binary operators, parameter-list should contain the operand on the right side of the operator (the operand on the left side is passed as this).

For the non-member operator overload function, the operand on the left side should be passed as the first parameter and the operand on the right side should be passed as the second parameter.

You cannot overload the #, ##, ., :, .*, or ? tokens.


private

Private data of a class can only be accessed by members of that class, except when friend is used. The private keyword can also be used to inherit a base class privately, which causes all public and protected members of the base class to become private members of the derived class.


protected

Protected data are private to their own class but can be inherited by derived classes. The protected keyword can also be used as an inheritance specifier, which causes all public and protected members of the base class to become protected members of the derived class.


public

Public data in a class are accessible to everyone. The public keyword can also be used as an inheritance specifier, which causes all public and protected members of the base class to become public and protected members of the derived class.


register

The register keyword requests that a variable be optimized for speed, and fell out of common use when computers became better at most code optimizations than humans.


reinterpret_cast
Syntax:
  reinterpret_cast<type> (object);

The reinterpret_cast operator changes one data type into another. It should be used to cast between incompatible pointer types.


return
Syntax:
  return;
  return( value );

The return statement causes execution to jump from the current function to whatever function called the current function. An optional value can be returned. A function may have more than one return statement.


short

The short keyword is a data type modifier that is used to declare short integer variables. See the data types page.


signed

The signed keyword is a data type modifier that is usually used to declare signed char variables. See the data types page.


sizeof

The sizeof operator is a compile-time operator that returns the size of the argument passed to it. The size is a multiple of the size of a char, which on many personal computers is 1 byte (or 8 bits). The number of bits in a char is stored in the CHAR_BIT constant defined in the <climits> header file.

For example, the following code uses sizeof to display the sizes of a number of variables:

  struct EmployeeRecord {
    int ID;
    int age;
    double salary;
    EmployeeRecord* boss;
  };

  ...

  cout << "sizeof(int): " << sizeof(int) << endl
       << "sizeof(float): " << sizeof(float) << endl
       << "sizeof(double): " << sizeof(double) << endl
       << "sizeof(char): " << sizeof(char) << endl
       << "sizeof(EmployeeRecord): " << sizeof(EmployeeRecord) << endl;

  int i;
  float f;
  double d;
  char c;
  EmployeeRecord er;

  cout << "sizeof(i): " << sizeof(i) << endl
       << "sizeof(f): " << sizeof(f) << endl
       << "sizeof(d): " << sizeof(d) << endl
       << "sizeof(c): " << sizeof(c) << endl
       << "sizeof(er): " << sizeof(er) << endl;

On some machines, the above code displays this output:

  sizeof(int): 4
  sizeof(float): 4
  sizeof(double): 8
  sizeof(char): 1
  sizeof(EmployeeRecord): 20
  sizeof(i): 4
  sizeof(f): 4
  sizeof(d): 8
  sizeof(c): 1
  sizeof(er): 20

Note that sizeof can either take a variable type (such as int) or a variable name (such as i in the example above).

It is also important to note that the sizes of various types of variables can change depending on what system you're on. Check out a description of the C and C++ data types for more information.

The parentheses around the argument are not required if you are using sizeof with a variable type (e.g. sizeof(int)).


static

The static data type modifier is used to create permanent storage for variables. Static variables keep their value between function calls. When used in a class, all instantiations of that class share one copy of the variable.


static_cast
Syntax:
  static_cast<type> (object);

The static_cast keyword can be used for any normal conversion between types. No runtime checks are performed.


struct
Syntax:
  struct struct-name : inheritance-list {
  public-members-list;    
  protected:
  protected-members-list;
  private:
  private-members-list;
  } object-list;

Structs are like `classes`, except that by default members of a struct are public rather than private. In C, structs can only contain data and are not permitted to have inheritance lists. For example:

   struct Date {
     int Day;
     int Month;
     int Year;
   };           

switch
Syntax:
  switch( expression ) {
  case A:
  statement list;
  break;
  case B:
  statement list;
  break;
  ...
  case N:
  statement list;
  break;
  default:
  statement list;
  break;
  }

The switch statement allows you to test an expression for many values, and is commonly used as a replacement for multiple if()...else if()...else if()... statements. break statements are required between each case statement, otherwise execution will "fall-through" to the next case statement. The default case is optional. If provided, it will match any case not explicitly covered by the preceding cases in the switch statement. For example:

   char keystroke = getch();
   switch( keystroke ) {
     case 'a':
     case 'b':
     case 'c':
     case 'd':
       KeyABCDPressed();
       break;
     case 'e':
       KeyEPressed();
       break;
     default:
       UnknownKeyPressed();
       break;
   }            

template
Syntax:
  template <class data-type> return-type name( parameter-list ) {
  statement-list;
  }

Templates are used to create generic functions and can operate on data without knowing the nature of that data. They accomplish this by using a placeholder data-type for which many other data types can be substituted.


this

The this keyword is a pointer to the current object. All member functions of a class have a this pointer.


throw
Syntax:
  try {
  statement list;
  }
  catch( typeA arg ) {
  statement list;
  }
  catch( typeB arg ) {
  statement list;
  }
  ...
  catch( typeN arg ) {
  statement list;
  }

The throw statement is part of the C++ mechanism for exception handling. This statement, together with the try and catch statements, the C++ exception handling system gives programmers an elegant mechanism for error recovery.

You will generally use a try block to execute potentially error-prone code. Somewhere in this code, a throw statement can be executed, which will cause execution to jump out of the try block and into one of the catch blocks. For example:

   try {
     cout << "Before throwing exception" << endl;
     throw 42;
     cout << "Shouldn't ever see this" << endl;
   }
   catch( int error ) {
     cout << "Error: caught exception " << error << endl;
   }            

true

The Boolean value of "true".


try

The try statement attempts to execute exception-generating code. See the throw statement for more details.


typedef
Syntax:
  typedef existing-type new-type;

The typedef keyword allows you to create a new type from an existing type.


typeid
Syntax:
  typeid( object );

The typeid operator returns a reference to a type_info object that describes `object`.


typename

The typename keyword can be used to describe an undefined type or in place of the class keyword in a template declaration.


union
Syntax:
  union union-name {
  public-members-list;    
  private:
  private-members-list;
  } object-list;

A union is like a class, except that all members of a union share the same memory location and are by default public rather than private. For example:

   union Data {
     int i;
     char c;
   };           

unsigned

The unsigned keyword is a data type modifier that is usually used to declare unsigned int variables. See the data types page.


using

The using keyword is used to import a namespace (or parts of a namespace) into the current scope.


virtual
Syntax:
  virtual return-type name( parameter-list );
  virtual return-type name( parameter-list ) = 0;

The virtual keyword can be used to create virtual functions, which can be overridden by derived classes.

If the function is specified as a pure virtual function (denoted by the = 0), it must be overridden by a derived class.


void

The void keyword is used to denote functions that return no value, or generic variables which can point to any type of data. Void can also be used to declare an empty parameter list. Also see the data types page.


volatile

The volatile keyword is an implementation-dependent modifier, used when declaring variables, which prevents the compiler from optimizing those variables. Volatile should be used with variables whose value can change in unexpected ways (i.e. through an interrupt), which could conflict with optimizations that the compiler might perform.


wchar_t

The keyword wchar_t is used to declare wide character variables. Also see the data types page.


while
Syntax:
  while( condition ) {
  statement-list;
  }

The while keyword is used as a looping construct that will evaluate the statement-list as long as condition is true. Note that if the condition starts off as false, the statement-list will never be executed. (You can use a do loop to guarantee that the statement-list will be executed at least once.) For example:

   bool done = false;
   while( !done ) {
     ProcessData();
     if( StopLooping() ) {
       done = true;
     }
   }