0% found this document useful (0 votes)
44 views7 pages

ENUM - Make U R Names As Data Types

http://comsciguide.blogspot.com/ With the help of enum keyword, one can declare their favorite names as integral data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views7 pages

ENUM - Make U R Names As Data Types

http://comsciguide.blogspot.com/ With the help of enum keyword, one can declare their favorite names as integral data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Keywords II

Enum :
An enum type is a special data type that enables for a variable to be
a set of predefined constants. The variable must be equal to one of the values
that have been predefined for it.
Thesyntaxis:
enum Tag_name {Variables_names}

For example:
enum tag_name{ value1, value2,...,valueN };

Here, tag_name is the name of enumerated data type. And value1,


value2,....,valueN are values of type tag_name.
If the first enumerator does not have an initializer (if it is not initialized
by the user), the associated value is zero. For any other enumerator
whose definition does not have an initializer, the associated value is the
value of the previous enumerator plus one.

Enum variables are the same size as an int variable. This is because each
enumerator is automatically assigned an integer value based on its
position in the enumeration list
In the above exapmle, By default, value1 will be equal to 0, value2

will be 1 and so on... But, the programmer can change the default values to
For references see : http://comsciguide.blogspot.com/

their own values.


//Changing the default value of enum elements

enum numbers {

zero = 0,
five = 5,
twentyone = 21,
thirty = 30 };

cout<<zero<<endl;
cout<<five<<endl;
cout<<twentyone<<endl;
cout<<thirty<<endl;

//output = 0
//output = 5
//output = 21
//output = 30

enum months { january = 1, february, march, april,


may, june, july, august,
september, october, november, december};

By creating the enum months, you have also created a new data type
called months. You can use this new data type as you might any other data
type. For example:
months jan ;
jan = january;

At the declaration time, we can also initialize the values with the
primitive operators(only a few).
enum Letters { A, B, C=-10, D, E=1, F, G=F+C};
//A=0, B=1, C=-10, D=-9, E=1, F=2, G=12

The enum constants can be assigned to other enumerators.


enum color { red, yellow, green = 20, blue };

For references see : http://comsciguide.blogspot.com/

color col = red;


int n = blue;

// n == 21

cout<<col;

// output =0;

Already declared enumerators can't be reassigned explicitly.


enum color { red = 0, yellow, green, blue };
color red = 11;

// compilation error

However, the compiler will not implicitly cast an integer to an enumerated


value. The following will produce a compiler error:
enum color { red = 0, yellow, green, blue };
color red1 = 11;

// compilation error

But there is one way to change enumerators values.This can be done


using the static_cast operator by forcing the compiler to put the value in
the enumerated data types.
enum color { red = 0, yellow, green, blue };
color red = static_cast<color>(11);
color red1 = static_cast<color>(11)

// error
// no error

Assigning between enumerators can be done but between integers and


enumerators results in compilation error. And enum also should be of the
same type.
enum color { red = 0, yellow, green, blue };
color red1 = 11;

// compilation error

color red1 = red;

// no error

For references see : http://comsciguide.blogspot.com/

enum color { red = 0, yellow, green, blue };


enum color1 {violet ,orange, black ,white };
color red1 = white;

// compilation error

Enumerated types are incredibly useful for code documentation and


readability purposes when you need to represent a specific number of
states.
enum Pincode
{

hyd = 500001,
krnl = 518001,
kdp = 516001,
jmd = 516434

};
Pincode getpincode()
{
if(city == hyderabad)
return hyd;
if(city == kurnool)
return krnl;
if(city == kadapa)
return kdp;
if(city == jammalamadugu)
return jmd;
}

This will be much easier to read than returning the number values.
Enumerators with class :
In gerenal, Enumerators are implicitly converted into the int data type. It
is also possible to have char, float etc thus preserving data type safety. They are
declared with enum class (or enum struct) instead of just enum.

For references see : http://comsciguide.blogspot.com/

For example

enum class biodata{ name, age, place, dob };

Each enum value is scoped with the name of the enum class. In other
words, to access the enum values, you must write:
enum class Colors {black, blue, green, cyan};
Colors mycolor;
mycolor = Colors::blue;
if (mycolor == Colors::green)
cout<<the color is green;

Enumerated types declared with enum class also have more control
over their underlying type. It may be any integral data type, such as
char, short or unsigned int, which essentially serves to determine the size of
the type. This is specified by a colon and the underlying type following the
enumerated type.
For example:
enum class biodata : int { dob, age};
enum class biodata : char{ name, place};

Try urself :
1.

#include<iostream>
using namespace std;
class s
{

For references see : http://comsciguide.blogspot.com/

public:
enum Pincode
{

hyd = 500001,
krnl = 518001,
kdp = 516001,
jmd = 516434

};
Pincode getpincode(string city)
{
if(city=="hyderabad")
return hyd;
if(city=="kurnool")
return krnl;
if(city=="kadapa")
return kdp;
if(city=="jammalamadugu")
return jmd;
}
};
int main()
{
s aa;
string p = "kurnool";
int a = aa.getpincode(p);
cout<<a;
return 0;
}
2.
#include<iostream>
using namespace std;
int main()
{

enum Color { RED, GREEN, BLUE};


Color r = RED;
switch(r)

For references see : http://comsciguide.blogspot.com/

{
case RED

: std::cout << "red\n"; break;

case GREEN : std::cout << "green\n"; break;


case BLUE

: std::cout << "blue\n"; break;

}
return 0;
}

For references see : http://comsciguide.blogspot.com/

You might also like