3a) explicitly assigned integer values
3b) not explicitly assigned a value
3c) explicitly assigned floating point values
3d) negative
3e) non-unique
3f) assigned the value of prior enumerators (eg. COLOR_MAGENTA = COLOR_RED)
C++ Tutorial Site
// define a new enum named Color
enum Color
{
// Here are the enumerators
// These define all the possible values this type can hold
COLOR_BLACK,
COLOR_RED,
COLOR_BLUE,
COLOR_GREEN,
COLOR_WHITE,
COLOR_CYAN,
COLOR_YELLOW,
COLOR_MAGENTA
};
// Declare a variable of enumerated type Color
Color eColor = COLOR_WHITE;
enum Color
{
COLOR_BLACK, // assigned 0
COLOR_RED, // assigned 1
COLOR_BLUE, // assigned 2
COLOR_GREEN, // assigned 3
COLOR_WHITE, // assigned 4
COLOR_CYAN, // assigned 5
COLOR_YELLOW, // assigned 6
COLOR_MAGENTA // assigned 7
};
Color eColor = COLOR_WHITE;
cout << eColor;
// define a new enum named Animal
enum Animal
{
ANIMAL_CAT = -3,
ANIMAL_DOG, // assigned -2
ANIMAL_PIG, // assigned -1
ANIMAL_HORSE = 5,
ANIMAL_GIRAFFE = 5,
ANIMAL_CHICKEN // assigned 6
};
int nValue = ANIMAL_PIG;
Animal eAnimal = 5; // will cause compiler error
Animal eAnimal = static_cast<Animal>(5); // compiler won't complain, but bad style
Animal eAnimal = COLOR_BLUE; // will cause compile error
int ParseFile()
{
if (!OpenFile())
return -1;
if (!ReadFile())
return -2;
if (!Parsefile())
return -3;
return 0; // success
}
enum ParseResult
{
SUCCESS = 0,
ERROR_OPENING_FILE = -1,
ERROR_READING_FILE = -2,
ERROR_PARSING_FILE = -3,
};
ParseResult ParseFile()
{
if (!OpenFile())
return ERROR_OPENING_FILE;
if (!ReadFile())
return ERROR_READING_FILE;
if (!Parsefile())
return ERROR_PARSING_FILE;
return SUCCESS;
}
if (ParseFile() == SUCCESS)
{
// do something
}
else
{
// print error message
}
Hungarian Notation is a naming convention in which the type and/or scope of a variable is used as a naming prefix for that variable. Fo...
Blocks (compound statements) A block of statements, also called a compound statement , is a group of statements that is treated by th...
In the previous lesson on basic inheritance in C++ , you learned that classes can inherit members and functions from other classes. In th...
Access functions An access function is a short public function whose job is to return the value of a private member variable. For ex...
Overloading the comparison operators is simple once you’ve learned how to overload the arithmetic operators. Because the comparison oper...
We have been initializing our class member data in the constructor using the assignment operator. For example: 1 2 3 4 5 ...
Initializing Arrays Because array variables are treated just like normal variables, they are not initialized when created. C++ provide...
Incrementing (adding 1 to) and decrementing (subtracting 1 from) a variable are so common that they have their own operators in C. There...
Even though the char data type is an integer (and thus follows all of the normal integer rules), we typically work with chars in a dif...
While relational operators can be used to test whether a particular condition is true or false, they can only test one condition at a ti...
0 comments: