Enumeration
Enumeration
Fundamentals of Enumeration
• Enumeration
– An enumeration is a user-defined data type that consists of
integral constants.
– It specifies all the possible values a variable of that type can
have. E.g. week enumeration can have seven days of week.
– To define an enumeration, keyword enum is used.
3
Fundamentals of Enumeration
• Example
– enum season {spring, summer, autumn, winter};
– Here season is name of enumeration.
– And, spring, summer and winter are values of type season.
– By default, spring is 0, summer is 1 and so on. You can change
the default value of an enum element during declaration (if
necessary).
4
Example
• Example
enum season
{
spring=0,
summer=4,
autumn=8,
winter=12
};
5
Day 4
7
1 // code 15
2 // Using enum.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8
9
10
11
12 enum suit{
13 club =0,
14 diamonds =10,
15 hearts =20,
16 spades =30,
17 } card;
18 int main()
19 {
20 card =club;
21
• cout<<“value is ”<< hearts;
• return 0;
• }
25
value is 20
8
1 // code 15
2 // Using enum.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8
9
10
11
12 enum suit{
13 club =0,
14 diamonds =10,
15 hearts =20,
16 spades =30,
17 } card;
18 int main()
19 {
20 card =club;
21
• cout<<“Size of enum variable ”<< sizeof(card) << “bytes.”;
• return 0;
• }
25