What are constants and enumerations useful for?

I have no code to give you but if you know what a constants and an enumerations is than you can help me.
So I was reading this book and it talked about constants and enumerations and how a constant is an unchangeable variable, and that enumerations are unsigned constant int and you can make multiple of them at once and it goes 0 1 2 3 for the variables values. But after that chapter I don't see them ever using them again for the next 300 pages or so. So I'm wondering what are they useful for and what can they really help you with.

Thank you.
The best use for constants and enumerations is to eliminate "magic numbers." If one has code that has a hard-coded number for doing calculations, and then you find out that the number you are using needs to change, you will need to find every hard-coded number and change that by hand!

But, if you have that number represented by a constant or an enumeration, you only have to change the value once and then recompile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

//    Bad way:

int AddOffset( int i )
    {
    return 10 + i ;

    }

int SubtractOffset( int i )
    {
    return i -  10;

    }

//    Better way:

enum 
    {
    offsetValue = 10 
    };


int AddOffset( int i )
    {
    return offsetValue + i;
    }

int SubtractOffset( int i )
    {
    return i - OffsetValue;

    }
I don't see them ever using them again for the next 300 pages or so.
Are you sure?
1
2
3
std::ifstream infile("some_file.txt", std::ios::ate); //Uses enumeration
infile.seekg(-1, std::ios::end); //likewise
std::cout.setf(std::ios::fixed); //Again 


Constant are used for date whcih will not change. It protects you from potential problems, helps with optimisation and makes it easier to reason about code. Most often const is used when passing stuff by const reference:
1
2
//const reference should be default approach to passing non-builtin types
void foo(const T& bar);
Other example is std::string::npos : it is const static value representing non-valid index in string and returnde by find function when element is not found.
PI is a constant, the value for PI will never change, although it is infinite and we may learn more decimal values for PI, but that's another conversation. If your writing a program using PI, you wouldn't want that value to change.

If your making a graphics program, the color Red, Black, White, can be expressed with numbers. You might want the user to modify the color, but if they reset RED, you wouldn't want that to ever change.
So enumerations and constants are mostly used for classes?
No. They are commongly used in C too which is known to lack of classes.

Enumerations are groups of logically bound named constants.
"So enumerations and constants are mostly used for classes?"

How you can say like this???
It measn you didnt get what is constants and Enum.

Its very basic , And it comes from C itself. Its nothing to do with classes.

:)
closed account (E0p9LyTq)
So enumerations and constants are mostly used for classes?


A couple of pages from the tutorial here you might want to read:

http://www.cplusplus.com/doc/tutorial/constants/
http://www.cplusplus.com/doc/tutorial/other_data_types/
Topic archived. No new replies allowed.