Enumeration example in c++ - programming enum

Lets get into the topic and discuss what is enumeration in C++. We have learned in previous post, How to increase your coding logical abil...

Lets get into the topic and discuss what is enumeration in C++. We have learned in previous post, How to increase your coding logical ability. An enumeration is a user-defined type whose value is restricted to one of several explicitly named constants(enumerators). Enumeration are defined using keyword: enum.

Example:
`enum seasons { spring, summer, autumn, winter };`
This code is a enum declaration. After declaration, you can define variable of type seasons. And this variable of type seasons can only have one of those 4 values. For example:
C++ program to define enumeration type and assign value to variable of that type.
``````#include <iostream>
using namespace std;

enum seasons { spring, summer, autumn, winter };

int main() {

seasons s;
s = autumn;   // Correct
s = rainy;    // Error
return 0;
}``````
In this program, an enum type seasons is declared with 4 enumerators (springsummerautumnwinter). Then, inside `main()` function, a variable s of type seasons is defined. This variable s can only store any one of four values (springsummerautumnwinter).
By default, the value of first enumerator is 0, second is 1 and so in. In this program, spring is equal to 0, summer is equal to 1 and autumn is 2 andwinter is 3.
One very important thing to remember is that, springsummer etc are not variables. They are treated as integers by compiler. Hence, once enumerators are defined, their value can't be changed in program.

### Example 1: C++ Enumeration

``````#include <iostream>
using namespace std;

enum seasons { spring, summer, autumn, winter };

int main() {

seasons s;

s = spring;
cout << "spring = " << s << endl;

s = summer;
cout << "summer = " << s << endl;

s = autumn;
cout << "autumn = " << s << endl;

s = winter;
cout << "winter = " << s << endl;

return 0;
}``````
Output
 C++ enumeration example 1

You can change the default value during enumeration declaration(after declaration, you cannot change it) and give them another value. Consider this example:

#### Example 2: C++ Enumeration

``````#include <iostream>
using namespace std;

enum seasons { spring = 34, summer = 4, autumn = 9, winter = 32};

int main() {

seasons s;

s = summer;
cout << "summer = " << s << endl;

return 0;
}``````
Output
 C++ enumeration example 2

That's all for the post I will tell you more about Enum in my future posts.

Name

ltr
item
MindxMaster: Enumeration example in c++ - programming enum
Enumeration example in c++ - programming enum
https://4.bp.blogspot.com/-BskONRkbJdQ/VemXJgou1sI/AAAAAAAAAgo/qrRYHKLaeMA/s1600/seasonns.png
https://4.bp.blogspot.com/-BskONRkbJdQ/VemXJgou1sI/AAAAAAAAAgo/qrRYHKLaeMA/s72-c/seasonns.png
MindxMaster
http://www.mindxmaster.com/2015/09/what-is-enumeration-in-c-with-example.html
http://www.mindxmaster.com/
http://www.mindxmaster.com/
http://www.mindxmaster.com/2015/09/what-is-enumeration-in-c-with-example.html
true
5332415103371288268
UTF-8