by John
Kopp
Welcome to About.com's C++ tutorial. This lesson will teach
you how to declare and use constants in C++. A
constant is similar to a variable in the sense that it represents a
memory location. It differs, as I sure you can guess, in that
cannot be reassigned a new value after initialization. In general, constants
are a useful feature that can prevent program bugs and logic errors. Unintended modifications
are prevented from occurring. The compiler will catch attempts to reassign
constants new values.
Defining Constants in C++
There are
three techniques used to define constants in C++. The first
technique comes from the C programming language. Constants may
be defined using the preprocessor directive, #define. The preprocessor is a program that
modifies your source file prior to compilation. Common
preprocessor directives are #include, which is used to include additional
code into your source file, #define, which is used to define a
constant and #if/#endif, which can be used to conditionally
determine which parts of your code will be compiled. The
#define directive is used as follows.
#define pi 3.1415 #define id_no 12345
|
Wherever the constant appears in your source file, the
preprocessor replaces it by its value. So, for instance, every
"pi" in your source code will be replace by 3.1415. The
compiler will only see the value 3.1415 in your code, not
"pi". The problem with this technique is that the replacement
is done lexically, without any type checking, without any
bound checking and without any scope checking. Every "pi" is
just replaced by its value. The technique is outdated, exists
to support legacy code and should be avoided.
Const
The second technique is to use
the keyword const when defining a variable. When used the
compiler will catch attempts to modify variables that have
been declared const.
const float pi = 3.1415; const int id_no = 12345;
|
There are two main advantages over the first technique.
First, the type of the constant is defined. "pi" is float.
"id_no" is int. This allows some type checking by the
compiler. Second, these constants are variables with a
definite scope. The scope of a variable relates to parts of
your program in which it is defined. Some variables may exist
only in certain functions or in certain blocks of code. You
may want to use "id_no" in one function and a completely
unrelated "id_no" in your main program. Sorry if this is
confusing, the scope of variables will be covered in a latter
lesson. I had a chicken and egg type of problem and my
solution was that the chicken is constant and the scope is an
egg.
Enumerations
The third technique is
called enumeration. An enumeration defines a new type and
limits the values of this type to a programmer defined set.
For example:
enum COLOR { RED, BLUE, GREEN}; enum SHAPE
{SQUARE, RECTANGLE, TRIANGLE, CIRCLE, ELLIPSE};
|
Each enumerated constant (sometimes called an enumerator)
has an integer value. Unless specified, the first constant has
a value of zero. The values increase by one for each
additional constant in the enumeration. So, RED equals 0, BLUE
equals 1, and GREEN = 2. SQUARE equals 0, RECTANGLE equals 1,
TRIANGLE equals 2 and so forth. The values of each constant
can also be specified.
enum SHAPE
{SQUARE=5,RECTANGLE,TRIANGLE=17,CIRCLE,ELLIPSE};
|
Here, SQUARE equals 5, RECTANGLE equals 6, TRIANGLE equals
17, CIRCLE equals 18 and ELLIPSE equals 19. The advantage of
enumerations is that if a variable is declared to be the type
of an enumeration, it has a type and its values are limited
and checked during compiling. Try compiling the below program.
What error does it contain?
#include <iostream> using namespace
std;
int
main() { enum color
{RED, GREEN,
BLUE}; color myColor =
5;
if (myColor ==
RED)
{ cout
<< "hot" <<
endl; } if
(myColor == BLUE)
{ cout
<< "cold" <<
endl; } if
(myColor == GREEN)
{ cout
<< "Is not easy being" <<
endl; }
return
0; } |
Corrected
Program with explanation
Previous
Articles
C++
Tutorial - See all lessons