Enumerations

Posts 1–7 of 7 · Page 1 of 1
Enumerations
Hello everyone,

I'm learning C++, but in my book (C++ Primer Plus 5th Edition) I came apon enumerations but I didn't really understand it.

Heres the paragraph if you want to see it:
http://pastebin.com/raw.php?i=qGhHSjaW

Can someone explain enums to me please.
Quote Originally Posted by Coders2020
An enumeration is a type that can hold a set of constant values that are defined by the user. Once you define an enumeration you can use it like integer types.E.g. enum {OFF, ON};The enum defines two integer constants called enumerators and these constants are assigned values by default. The value assigned to OFF = 0 and ON = 1. The above is an unnamed enumeration, however an enum can be named. E.g. enum STATE {OFF, ON};Each enumeration is a distinct type and the type of the enumeration is the enumerator. For example OFF is of type STATE. An enumerator can optionally be initialized by a constant expression of integral type. E.g. enum {WHITE = 3, BLACK = 5}; A value of an integral type can be converter to an enumeration type. This needs to be done explicitly. The results are undefined unless the value is within the range of the enumeration.E.g. enum COLORS {RED = 1; BLUE = 3, WHITE = 7};COLORS c1 = COLORS (5); All enumerators are converted to integral type for any arithmetic operation.
There you go. ^^
Wouldnt this be the same as DEFINE?

The only difference that I see is that Enumerations increase in number when you dont specify otherwise.
Quote Originally Posted by aanthonyz View Post
Wouldnt this be the same as DEFINE?

The only difference that I see is that Enumerations increase in number when you dont specify otherwise.
nope, you can give them a certain value for ie:
Code:
enum MyEnum{
    member1 = 0x45,
    member2 = 0xH8
};
And also #define, declares a value which isn't constant. On a enum all the values that are hold are constants and will never change....
Enumeration Allows you to Create your own Data types in which the Data type is constant

Some examples:

[Highlight=cpp]

enum eMine {
cValue0,
cValue1,
cValue2,
cValue3
};

cout << cValue0 << "" << cValue1 << "" << cValue2 << "" << cValue3 ;

[/Highlight]

And it will output 0,1,2,3

By default its going to assign cValue0 the number 0 And cValue1 the number 1 and so on so on..
But you can assign them your own values like so

[Highlight=cpp]

enum eMine {
cValue0 = 4,
cValue1 = 7,
cValue2 = 10,
cValue3 = 12
};

cout << cValue0 << "" << cValue1 << "" << cValue2 << "" << cValue3 ;


[/Highlight]

And output will be as you see in the enumeration
Quote Originally Posted by whit View Post
Enumeration Allows you to Create your own Data types in which the Data type is constant

Some examples:

[Highlight=cpp]

enum eMine {
cValue0,
cValue1,
cValue2,
cValue3
};

cout << cValue0 << "" << cValue1 << "" << cValue2 << "" << cValue3 ;

[/Highlight]

And it will output 0,1,2,3

By default its going to assign cValue0 the number 0 And cValue1 the number 1 and so on so on..
But you can assign them your own values like so

[Highlight=cpp]

enum eMine {
cValue0 = 4,
cValue1 = 7,
cValue2 = 10,
cValue3 = 12
};

cout << cValue0 << "" << cValue1 << "" << cValue2 << "" << cValue3 ;


[/Highlight]

And output will be as you see in the enumeration
Thanks I think I get it now but just to be clear, it's like a struct but with intergers right?
Quote Originally Posted by Nathan View Post
Thanks I think I get it now but just to be clear, it's like a struct but with intergers right?
yup . . . =)
Posts 1–7 of 7 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?