answersLogoWhite

0

Enumerated data types are a set of user-defined constants called enumerators that provide context to a range of values. For instance, if you define the following constants:

static const int Monday=0;

static const int Tuesday=1;

// ...

static const int Sunday = 6;

static const int January=0;

static const int February=1;

// ...

static const int December=12;

Then there's nothing to prevent you from coding the following:

int var1 = January;

int var2 = Tuesday;

// ...

int var3 = var1 + var2;

What exactly does var3 represent? A month? A day? Or something else entirely? The problem is the compiler can't help you because you've not actually done anything wrong. As far as the compiler is concerned, all you've done is add two ints together to produce a third int. Whether it makes sense or not is another matter altogether.

Enumerations exist to prevent this sort of problem.

enum day { Monday, Tuesday, ..., Sunday };

enum month { January, February, ..., December };

month var1 = January;

day var2 = Tuesday;

// ...

int var3 = var1 + var2;

Now the compiler will tell you there is no suitable plus operator that accepts an l-value of month and an r-value of day.

Enumerators have an implied type of signed int, but you can also specify an alternative type, provided it is an intrinsically primitive integral type, such as int, char, or short, whether signed or unsigned. However, even when typed, you cannot use any operators upon them unless you specifically provide an operator overload to cater for them. Thus the following will not work:

for( day d=Monday; d<=Sunday; ++d )

// ...

Without a suitable operator overload for the prefix increment operator, you must use an explicit cast, such as follows:

for( int i=Monday; i<=Sunday; ++i )

{

day d = reinterpret_cast<day>(i);

// ...

}

Although you've got to work a bit harder to use enumerators, the point is that the additional work is required in order to prevent problems such as those highlighted at the start of this answer. Enumerators enlist the compiler to help you spot errors, forcing you to make a conscious decision whenever you want to override the compiler's help, but ultimately ensuring your code is as robust as possible.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Which data type is the only one that can be used in calculations?

Which data type is the only one that can be used in calculations?


What are the applications of void data types inc plus plus?

void data type is used in function declaration. It can be explained by examle void add(int,int); this will tell the compiler that no value is going to be returned by the function. int add(int,int); this indicates that an integer type value will be returned by the function


What type of data do pareto charts used for calculation?

Continuous data


Which data type can store any data?

There is no such data type. However, when we use user-defined data types of our own type, then that type of data can be stored in a variable. So as a term, you may say that user-defined data type can store any data. As the data-type used in any variable will be depending upon us.


Application of void data type in c plus plus programming language?

The voiddata type is used when a function doesn't return any value, and/or when it has no parameters at all. Pointer type 'void *' is a generic pointer.A void pointer is used when it needs to be assigned to different data types later on in a program. Since it avoids type checking, void pointers should be used with care.


Suggest the data type that can be used for initializing alphanumeric elements in a c programmed array .Also tell you the compiler that can understand the data type.?

Data Type : - It is used to identify the type of data. 'C' Language has a large no of data type, Thus it is also known by rich data type language: Data type are generally classified in three group: 1: Fundamental data type 2 Derived Data Type : 3 Use defined data type; 1 Fundamental data type: Fundamental data type includes i) The int data type: The data type int can store integer value only for eg. 14, 45, 78 declaration: int a,b; here we can store any value in variable a & b. ii) Char Data Type : The data type char can store character value only which is enclosed with single quote for e.g. 'c' declaration : char x = 'c' iii) Float Data Type:


The monetary data type is used for fields that contain only monetary data?

FALSE


Which data type is used in order to fetch stream of data from network or file?

char


What data type is used for a zip code?

number


What is the improved array definition?

An array is used to store data having the same data type.


How many techniques used to discard data?

Three type of techniques used


Why String data type cannot be used as built-in data type in C?

Because it isn't a built-in data-type in C. Other examples that aren't built-in data-types: complex numbers, binary trees, associative-arrays.