More Data Types

There are more primitive data types in C++ which are variation on the ones described above. They are as follows:

  • unsigned char -- takes non-negative values twice as high as normal chars.
  • unsigned int -- similarly, takes on non-negative values up to twice the highest value of normal ints.
  • long -- or long int, are like ints, but can take on much larger values.
  • unsigned long int -- takes on very large non-negative values
  • long double -- takes on very large real number values.
  • bool -- takes on either the value true or the value false.

Constant Variables and Enumerated Types

You will often want to have a variable whose value can not be changed. For instance, it is generally considered stylistically poor to have constants without explanation, as in the following:

float area = 3.1415 * radius * radius;

Not only can the introduction of 3.1415 be confusing, but it is like that you will want to use the same value elsewhere in your program. It is better to assign the value to a constant variable using const:

const PI = 3.1415; float area = PI * radius * radius;
This syntax replaces C's #define syntax for defining constants. One advantage of this is that constant values have types in C++ and can therefore be checked at compile time.

Sometimes you may want to forget entirely that your variables are being represented by numbers. In keeping track of the days of the week, you might like to write something like:

const int SUNDAY = 0; const int MONDAY = 1; const int TUESDAY = 2;

etc.

This will work, but C++ lets you more easily create your own enumerated type as follows: