Using Constants - C++

C++ has two types of constants. You’ve already seen one of these, the literal constant. A literal constant is the same as a literal (refer to the section “Sorting Out the Relationship between Variables and Memory,” earlier in this chapter, for more on literals). Examples of literal constants are the number 2 and the string “Hello”. These are defined as constants because you cannot change their values.

The second type of constant is the symbolic constant. The symbolic constant is like a variable in many ways except that its value cannot be changed. At the top of your program, you might define the word PI to be the constant for 3.14. When the code is compiled, every instance of the identifier PI will be replaced by the number 3.14, which means that as far as the computer is concerned, there is no difference between using a symbolic constant and a literal constant. But before you start wondering about why and when to use symbolic constants, you need to learn how to use them.

You can declare a constant two ways. The first way is to use the #define directive. (A directive is a line of code that is executed by the compiler immediately before compiling the rest of the code.) The syntax for this directive is as follows:

CONSTNAME is the identifier that you want the constant to have, and value is the value that you want to assign to it. Notice that there is no semicolon at the end of the line, just as with the #include directive. A directive is not a C++ executable statement, but a precompile directive that the compiler follows. The #define directive tells the compiler to replace all occurrences of CONSTNAME in the program with its value. The compiler, after making the replacement, compiles the code. In your program, you can now use CONSTNAME wherever you would have put the literal constant. For example, you can declare a constant for pi:

Now, if your code has something like

you can use the constant instead:

The advantages of using constants are immediately obvious in terms of readability. If you use the number 3.14159, it is not immediately clear that you mean pi; but if you use a constant and call it PI, what you are referring to is crystal clear.

In the Real World

Imagine that you are the CEO of a major corporation that has been paying each employee $10.78 per hour, and you give everyone a raise to $11.78 per hour. You don’t want to go through the thousands of entries in your payment program. Using a constant to store your employees’ wages, you must change the constant in only one place, which saves you, and the corporation, a lot of time and effort.

The second way to define a constant is by using the const keyword. The syntax is almost exactly the same as the syntax for declaring a #define:

const constType CONSTNAME value;

The key difference here is that with const, you declare the type of your constant, which can be any of the possible variable types. This difference provides an advantage over #define because of type-checking. The compiler can now check to make sure that you are using your constant where use of its type is allowed. For example, if you make a string constant by using

or by using

you can use the constant’s name in code as a replacement for the string “Hello”.

Remember though that const string HELLO has type-checking, which means that you can use const string HELLO only where you would normally use a string. However, #define HELLO will simply try to make it work with some weird results:

The constant string declared with const will have a compile error, but the one declared with #define will not. If the compiler picks up your problems, you won’t have to try to find the errors yourself. This is incredibly advantageous for large applications.

Using the second method, the pi example is as follows:

Hint

Remember to initialize constants when you create them. After that, the value cannot change. If you don’t initialize the constant at the time of creation, you will get an error when you attempt to set the constant’s value.

Using constants offers numerous advantages. Suppose that you type a number, such as 3.14, throughout the program every time you need to use the value of pi. Then you realize that your application requires a higher a directive to define pi as 3.14 and later need to increase the precision, you will only need to change the #define directive and recompile the program.

If you have a really long number (such as pi to 22 decimal places), typing it every time that you use it will also be annoying—and if you are like us, you might easily make a mistake. Using a constant simplifies this situation considerably.

The Circle Game

For tomorrow’s math assignment, you must calculate the area and circumference of a circle. Well, maybe we can help. In the following program, we do both by using constants. You use a constant to store PI (3.141592). Then the user only has to enter the radius into the program, and the program will calculate both the area and the circumference of the circle. Remember that the user’s input should be stored as float so that the user can enter decimal numbers as a radius. Excellent; now it’s time to build the program:

//2.3 - The Circle Game - Dirk Henkemans and Mark Lee - Premier Press


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C++ Topics