November 2001
Beginner
1128 pages
29h 12m
English
Now let's return to the topic of symbolic names for constants. A symbolic name can suggest what the constant represents. Also, if the program uses the constant in several places and you need to change the value, you can just change the single symbol definition. The note about #define statements earlier in this chapter (“Symbolic Constants the Preprocessor Way”) promised that C++ has a better way to handle symbolic constants. That way is to use the const keyword to modify a variable declaration and initialization. Suppose, for example, that you want a symbolic constant for the number of months in a year. Enter this line in a program:
const int MONTHS = 12; // Months is symbolic constant for 12
Now you can use MONTHS in a ...
Read now
Unlock full access