< Prev
Next >



Enums in C++





A C++ Enumeration(Enums) defines a class type through which we can define a list of constants. These constant values are also called enumerators, where all enumerators are a type of its enumeration. For example - let's declare an enumeration Fruits and some its enumerators.

 enum Fruit {apple, pineapple, watermelon}

In this code above, we have declared an enumeration Fruits and its 3 enumerators such as - apple, pineapple, watermelon. By default, the constants/enumerators of an enum are assigned an int value, starting with 0, such as -




Features of Enums






A simple enumeration example


// Enumeration example

#include<iostream>

using namespace std;

enum Cities
{ 
Frankfurt, NewYork, Sydney, Tokyo		//enumeration's enumerators/constants
};


int main()
{
cout<< Frankfurt <<"\n";
cout<< NewYork <<"\n";
cout<< Sydney <<"\n";
cout<< Tokyo;
}


Output is :


0
1
2
3


Program Analysis







An anonymous enumeration


C++ even allows us to create an anonymous enumeration i.e. having no name.

// Enumeration example

#include<iostream>

using namespace std;

//Anonymous enumeration
enum
{ 
Frankfurt, NewYork, Sydney, Tokyo		//enumeration's enumerators/constants
};


int main()
{
cout<< Frankfurt <<"\n";
cout<< NewYork <<"\n";
cout<< Sydney <<"\n";
cout<< Tokyo;
}


Output is :


0
1
2
3


In the example above, we have created an anonymous enumeration i.e. an enumeration with no name., where enumerators such as Frankfurt, NewYork, Sydney, Tokyo are types of an anonymous enumeration.




Creating new enumerators


We could even create new enumerators, outside the scope of their enumeration. But an important point to remember is that these newly declared enumerators will be given random int values, by default.

Though, we could even assign or initialize these newly created enumerators with the values of already existing enumerators(which were declared within the scope of enumeration scope). Let's see an example.

// Enumeration example

#include<iostream>

using namespace std;

//Enumeration
enum Cities
{ 
Frankfurt, NewYork, Sydney, Tokyo		//enumeration's enumerators/constants
};


int main()
{
//Creating two new enumerators of type Cities, outside its scope
Cities LasVegas, Berlin;


//But by default, these enumerators are given random int values
cout<< LasVegas << "\n";
cout<< Berlin << "\n";

//Assigning these enumerators the values of pre-defined enumerators
Berlin = Frankfurt;
LasVegas = NewYork;

//Printing the new values assigned to the two new enumerators
cout<< Berlin <<"\n";
cout<< LasVegas << "\n";

//Creating a new enumerator and initializing it with a value of enumerator NewYork
Cities Atlanta = NewYork;
cout<< Atlanta <<"\n";
}


Output


4201083
2686824
0
1
1







Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Destructor in C++
Arithmetic Operator in C++ >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2