C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming

 < Day Day Up > 


A constant is an object in your program that you intend to remain unchanged during its lifetime. The use of constants can vastly improve the readability and maintainability of your code.

You can declare constants in C++ using the keyword const. Constants have to be defined at the point of declaration. The following statement gives an example of a constant declaration and definition:

const int MAX_COUNT = 25;

There is an exception to the define-at-the-point-of-declaration rule, and that is when you are declaring constants for use in classes or structures. In chapter 11 I will show you how to use constructors to define class constant members.

Once you have defined the constant, any attempt to change its value will be met with disapproval from the compiler.


 < Day Day Up > 

Категории