C++ Enumeration
enumeration are used to define the constants integers. Or we can say there are combined group of integers. enumeration are simplest user defined data type. There are defined by following syntax.
//declaration of enumeration
enum tag_name{
//enum body
//constant_variables
}variable_list;
key point | overview |
---|---|
enum | keyword |
tag_name | Name of enum |
constant_variables | list of integer constant variables |
variable_list | enum variable list (optional) |
For example
/*C++ Enumeration (enum) example.*/
#include<iostream>
using namespace std;
//defined enumeration
enum wallet{
//constant variables
coins=10,
rupee=100,
dollar=73
};
int main(){
//display enumeration variable value
cout<<" coins :"<<coins<<endl;
cout<<" rupee :"<<rupee<<endl;
cout<<" dollar :"<<dollar<<endl;
return 0;
}
Output
coins :10
rupee :100
dollar :73
Note that every enumeration variable are separated by comma. and end of this by semicolon.
Behavior of enumeration
If enumeration data member are not provide any integer value. Then compiler are start assigning default value to those data member.see example.
/*enum demonstrate default value.*/
#include<iostream>
using namespace std;
//defined enumeration
enum Number{
//constant variables
zero,//assign default value is 0
one, //assign default value is 1
two,//assign default value is 2
three,//assign default value is 3
four,//assign default value is 4
five,//assign default value is 5
};
int main(){
int index=zero;
for(index;index<=five;index++){
cout<<index<<endl;
}
return 0;
}
Output
0
1
2
3
4
5
Note that default value are start with zero. and other data member values are increment by previously variables. see another example.
/*enum demonstrate default value.*/
#include<iostream>
using namespace std;
//defined enumeration
enum candy{
taffy=5,
sugar_free, //default value is 6
hard,//default value is 7
little=10,
gum,//default value is 11
chocolates, //default value is 12
jelly=2,
lollipops //default value is 3
};
int main(){
//print values of candy enumeration
cout<<" sugar_free candy :"<<sugar_free<<endl;
cout<<" hard candy :"<<hard<<endl;
cout<<" lollipops candy:"<<lollipops;
return 0;
}
Output
sugar_free candy :6
hard candy :7
lollipops candy:3
We can create enumeration variables and assign the values in following ways. see the example.
/*enum demonstrate default value.*/
#include<iostream>
using namespace std;
//defined enumeration
enum candy{
taffy=5,
sugar_free,//default value is 6
hard,//default value is 7
little=10,
gum,//default value is 11
chocolates,//default value is 12
jelly=2,
lollipops//default value is 3
};
int main(){
//create variable of enumeration
candy favorite=jelly;
//assign 4 to enum variable
candy forever=candy(4);
//static_cast
candy tasty=static_cast<candy>(10);
cout<<favorite<<endl;
cout<<forever<<endl;
cout<<tasty;
return 0;
}
Output
2
4
10
We cannot define direct integer value to enumeration variables. compiler are not implicit conversion to integer value. there are need to type casting. above program are provide three way to assign value of enumeration variables. see examples.
#include<iostream>
using namespace std;
enum book{
c=10,
cpp=20,
java=30
};
int main(){
book b1;
cin>>b1;//error of this line
cout<<b1;
return 0;
}
Error like this
error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream}’ and ‘book’)
cin>>b1;//error of this line
Scope of enumerate are global that means we cannot declared two same name of two different enumeration. see example.
enum Webcolor{
pink,
green,
blue
};
enum Homecolor{
yellow,
orange,
red,
blue//same name
};
int main(){
return 0;
}
Compilation error
error: 'blue' conflicts with a previous declaration
blue//same name
^~~~
note: previous declaration 'Webcolor blue'
blue
but two enum name are same values are possible. look at view this example.
#include<iostream>
using namespace std;
enum computer{
hp=100,
dell=100,//same value
acer=100,//same value
};
int main(){
cout<<"hp : "<<hp<<" dell :"<<dell<<" acer :"<<acer;
return 0;
}
Output
hp : 100 dell :100 acer :100
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment