Advertisement



< Prev
Next >



C++ Enums





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 within an enumeration are a type of that specific 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


//C++ 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. an enumeration having no name.

//C++ 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 this anonymous enumeration.


Advertisement




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.

Note: 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.

//C++ 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 -





< Prev
Next >
< Destructor in C++
Arithmetic Operator in C++ >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement