Do you like to give values to a variable according to your program? For example, if you crave to know the direction of wind and would like to save the names of directions in the variable then it is good to get the help of an integer and defined principles:
#define SOUTH_WIND 1
#define EAST_WIND 2
#define WEST_WIND 3
#define NO_WIND 4
int wind_direction = NO_WIND;
Here the defect in this method can be described as; it can not tell us that we have given a wrong value. So, enumerated types are used which can get data according to the instructions. The values are defined as constants.
Whenever you write an enumerated category, you have to write the very name of fresh type and also the type of values it may have.
Here, the use of _t shows that it is a type. Even the text editor can draw attention to the fresh type.
Here, the variale wind_directions_t is determined where the range of values will be 5:
wind_direction = 453; // Compiler has found an error!
Message to the users of C language:
If you are going to utilize enumeration in C language then you have to declare it like this: enum wind_directions_t wind_direction = NO_WIND;
It is up to you to keep the values by default where the initial value will be zero and will keep on increasing one by one. You can even set the values be changed by you.
It is possible to overturn this by adding values which are unambiguous:
Have you thought about the reason of giving unambiguous values to members of enumerated type? The reason is; if these values have no contact with the exterior world then there will be no reason of defining specifically. If it has connection with the outer world then you have to stipulate it.
If you print an enum which is by default then the out put will be in integer.
Designation of Enums
Another problem with the enum is that it can signify anything when we apply the constant enum. The other is that it can not differentiate among values to which lists they belong to.
The resolution to such troubles is to write the very name of enum with the name of constants.
We can give enums to any int without using a cast. The reason is that enums are just like integers.
It can be:
int my_wind = wind_direction;
An overturn can be done in C++ with the use of type cast. Typecast should be used according to the situation. Otherwise, it will create errors.
std::cin >> static_cast( wind_direction );
If we use typecast on all places, then the user will become allowed to give a value irrespective to the function. The way out of this problem is to ask user to give input in string and after that select the related constant for that input.
std::cout << std::endl;
if ( user_wind_dir == "EAST" )
wind_dir = EAST_WIND;
else if ( user_wind_dir == "WEST" )
wind_dir = WEST_WIND;
else if ( user_wind_dir == "NORTH" )
wind_dir = NORTH_WIND;
else if ( user_wind_dir == "SOUTH" )
wind_dir = SOUTH_WIND;
else if ( user_wind_dir == "NONE" )
wind_dir = NO_WIND;
std::cout << "It is not a suitable way!" << std::endl;
If you have to switch from old to novel codes, polymorphism is used. If you have enums in your program then by including setting_t, you will become able to save new enums in the list.
If you are going to use this method then you have to save enum in form of int. you can get them back with the use of typecast.