symbolic constant in C++
There are two ways of creating symbolic constant in c++
1. using the qualifier constant
2. defining a set of integers constants using enum keyword
in the both c and c++ any value declared as constant(const) can not be modified by the program in any way. however, there are some differences in implementation. in c++ we can use const in constant expression, such as
This would be illegal in c. const allows us to create typed constants instead of having to use #define to create constants that have no type information with long and short, if we use the const modifier alone, it set defaults to int , let us take example
means
const int size=10;
The named constant are just like variables except that their values cannot be changed. c++ requires a const to be initialized. ANSI C does not required an initializer, if none is given it initialize the const to 0
The scoping of const values differs. A const in c++ programming defaults to the internal linkage and therefore
it is local to the file where it is declared. In ANSI C PROGRAMMING, const values are global in nature. They are visible outside the file in which they are declare. however they can be made local
by declaring them as static. To give a const value an external linkage so that it can be referenced from another file, we must explicitly define it as an extern in c++
example
another mothod of naming integer constants is by enumeration as under
enum{x,y,z};
this defines x,y and z as integer constant with values 0,1 and 2 respectively. this is equivalent to
so we can also assign integers values to x,y and z explicitly
example