You have often heard about "enumeration" in C++.Today I will explain this concept with the help of articles taken from different websites.
enum enum-type-name { enum-list };
//... (and somewhere below)
enum enum-type-name enum-variable;
Of course, in the second case enum-type-name cannon be omitted.
Definition of enumeration
Enum(enumeration) is a user-defined type consisting of a set of enumerators( enumerator --- named integer constant).
The idea behind enumerated types is to create new data types that can take on only a restricted range of values. Moreover, these values are all expressed as constants rather than magic numbers--in fact, there should be no need to know the underlying values.
Declaration of enumeration
The enum is declared as:
enum enum-type-name { enum-list } enum-variable;
In this form, enum-type-name is optional. However, if you want to use enum type in several places, it is better to use another way of enum declaration:enum enum-type-name { enum-list } enum-variable;
enum enum-type-name { enum-list };
//... (and somewhere below)
enum enum-type-name enum-variable;
Of course, in the second case enum-type-name cannon be omitted.
An example is given below which will show you how to declare enumeration in C++.
enum e_acomany
{
Audi,
BMW,
Cadillac,
Ford,
Jaguar,
Lexus,
Maybach,
RollsRoyce,
Saab
};
Below is a sample code that will help you to undersatnd this concept.
enum e_acomany
{
Audi,
BMW,
Cadillac,
Ford,
Jaguar,
Lexus,
Maybach,
RollsRoyce,
Saab
};
Below is a sample code that will help you to undersatnd this concept.
Source code
#include<iostream>
using namespace std;
enum colours{blue,red};
int main( )
{
colours c;
c=blue;
cout<<"Blue is at number:"<<c<<endl;
c=red;
cout<<"Red is at number:"<<c<<endl;
return 0;
}
Output
Blue is at number:0
Red is at number:1
Credits
For this blog,I have taken help from the following websites:
No comments:
Post a Comment