enum

R

Roger

enum status { CONTINUE, WON, LOST }

What do we really need to use enum for? I'm not sure what the use of
it is really for... Maybe I just don't quite understand. My textbook
says that these enumeration constants are integers and start at 0 if
you don't assign the first constant in the enum. So, in this case,
CONTINUE is 0, WON is 1, and LOST is 2.

Why cant we just use integers instead? Why enum???

Many thanks for the help :)
 
J

jalina

Roger a écrit :
enum status { CONTINUE, WON, LOST }

What do we really need to use enum for? I'm not sure what the use of
it is really for... Maybe I just don't quite understand. My textbook
says that these enumeration constants are integers and start at 0 if
you don't assign the first constant in the enum. So, in this case,
CONTINUE is 0, WON is 1, and LOST is 2.

Why cant we just use integers instead? Why enum???

Many thanks for the help :)

Because it is more readable. Compare:

if (current_status == WON) cout << "Great";
else cout << "Better luck next time";

and

if (current_status == 1) cout << "Great";
else cout << "Better luck next time";

J.
 
N

Neelesh Bodas

enum status { CONTINUE, WON, LOST }

What do we really need to use enum for? I'm not sure what the use of
it is really for... Maybe I just don't quite understand. My textbook
says that these enumeration constants are integers and start at 0 if
you don't assign the first constant in the enum. So, in this case,
CONTINUE is 0, WON is 1, and LOST is 2.

Why cant we just use integers instead? Why enum???

Many thanks for the help :)

Enums are 'named' constants.

(a) If you want to work with a predefined set of values, you may
collect them in an enum

Eg: say for a restaurant, glasses can only be of 4 different sizes:
Small, Medium, Large and Jumbo. In such a case, using a simple 'int'
is too much - we would ideally that a variable of type Glass can only
have one of the 4 values mentioned above. The easiest solution for
this is to define an enum:

enum GlassSize { SMALL, MEDIUM, LARGE, JUMBO };

Glass g = SMALL; //fine
Glass h = 12; //Error, int cannot be implicitly converted to an Enum
Glass r = EXTRALARGE; //Error, what is EXTRALARGE???

(b) Many times a programmer finds that using names instead of plain
integers helps readability of the code:
eg:

enum Color { Red, Green , Blue }

Color getFavColor()
{
return Red;
}

Is much more readable than:

int getFavColor()
{
return 0;
}

You may refer http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.19
for further basic information about enumerated types.

-N
 
K

Kai-Uwe Bux

Roger said:
enum status { CONTINUE, WON, LOST }

What do we really need to use enum for? I'm not sure what the use of
it is really for... Maybe I just don't quite understand. My textbook
says that these enumeration constants are integers and start at 0 if
you don't assign the first constant in the enum. So, in this case,
CONTINUE is 0, WON is 1, and LOST is 2.

Why cant we just use integers instead? Why enum???

In many cases, you can :)

However, there are some tricks that you cannot do with integer constants.
IMHO, the most important feature of enums is that they create honest to god
types and not just aliases for the underlying arithmetic type. That implies
that you can use overload resolution based on the enum type. This is
usefult, for instance, in overloading operator<< to output the enum values
in text form to streams.

#include <iostream>
#include <ostream>
#include <cassert>

enum status { down, up };

std::eek:stream & operator<< ( std::eek:stream & ostr,
status st ) {
if ( st == down ) {
return ( ostr << "down" );
}
if ( st == up ) {
return ( ostr << "up" );
}
assert ( false );
}

int main ( void ) {
status st = up;
std::cout << up << '\n';
}

Sometimes, it is important or at least useful that constants have a type of
their own. In those case, enums are a nice thing to have. In other cases,
you can of course get away with integers.


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Kai-Uwe Bux said:
In many cases, you can :)

I was a little brief here. I meant, in many cases, you can use integer
constants instead of enums:

int const CONTINUE = 0;
int const WON = 1;
int const LOST = 2;


However, there are some tricks that you cannot do with integer constants.
IMHO, the most important feature of enums is that they create honest to
god types and not just aliases for the underlying arithmetic type. That
implies that you can use overload resolution based on the enum type. This
is usefult, for instance, in overloading operator<< to output the enum
values in text form to streams.

#include <iostream>
#include <ostream>
#include <cassert>

enum status { down, up };

std::eek:stream & operator<< ( std::eek:stream & ostr,
status st ) {
if ( st == down ) {
return ( ostr << "down" );
}
if ( st == up ) {
return ( ostr << "up" );
}
assert ( false );
}

int main ( void ) {
status st = up;
std::cout << up << '\n';
}

Sometimes, it is important or at least useful that constants have a type
of their own. In those case, enums are a nice thing to have. In other
cases, you can of course get away with integers.

And this also should have been "get away with integer constants".


Best

Kai-Uwe Bux
 
R

Roger

I was a little brief here. I meant, in many cases, you can use integer
constants instead of enums:

int const CONTINUE = 0;
int const WON = 1;
int const LOST = 2;









And this also should have been "get away with integer constants".

Best

Kai-Uwe Bux

I think I understand this a little bit better now. Here's something I
did under Visual C++ 6.0 (yes, it's very basic... I'm still
learning :) )

#include <iostream.h>

int main() {
enum Number { ZERO, ONE, TWO };

cout << "Hello, try inputing 0,1, or 2. Just try!\n";

int input;
cin >> input;

switch (input) {
case ZERO:
cout << "ZERO!\n";
break;
case ONE:
cout << "ONE!\n";
break;
case TWO:
cout << "TWO\n";
break;
default:
cout << "TRY AGAIN!\n";
break;

}
return 0;
}

So if the user inputs 0, the program will take it as "ZERO" and so
on...
 
I

Ian Collins

Roger said:
I think I understand this a little bit better now. Here's something I
did under Visual C++ 6.0 (yes, it's very basic... I'm still
learning :) )

#include <iostream.h>
If you are still learning, learn the correct header, <iostream>
 
J

Juha Nieminen

Roger said:
Why cant we just use integers instead? Why enum???

Personally I like enums for two reasons:

1) They are a concise way of creating a series of related constants.
This is especially true if it's defined inside a class. For example,
instead of having to write:

class A
{
static const int X = 0, Y = 1, Z = 2;
};

you can simply write

class A
{
enum { X, Y, Z };
};

2) As mentioned in another post, an enum creates a new type. You can
make eg. functions take parameters of that type, you can overload
functions depending on the enum type, etc. This makes the code
cleaner and safer (because if you try to give an int to a function
taking the enum type you will at least get a warning, probably an
error).
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I think I understand this a little bit better now. Here's something I
did under Visual C++ 6.0 (yes, it's very basic... I'm still
learning :) )

I would recommend you to get a better compiler, VC++ 6 is very old and
not very good. You can download Visual C++ 2005 Express from MS site,
it's one of the better C++ compilers out there.
 
S

Scofield

Erik Wikström 写é“:
I would recommend you to get a better compiler, VC++ 6 is very old and
not very good. You can download Visual C++ 2005 Express from MS site,
it's one of the better C++ compilers out there.
i think g++ is more suitable
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Trouble calling a function with enum parameter 3
Rock paper scissors in python with "algorithm" 1
Scoped enum question 21
ANN: Python 3 enum package 0
PWM Issues 0
enum as bitfields 11
enum 6
Enum oddity 14

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top