1. 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.
The syntax is :
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 it’s
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
Also See more posts : www.comsciguide.blogspot.com
2. their own values.
//Changing the default value of enum elements
enum numbers { zero = 0,
five = 5,
twentyone = 21,
thirty = 30 };
cout<<zero<<endl; //output = 0
cout<<five<<endl; //output = 5
cout<<twentyone<<endl; //output = 21
cout<<thirty<<endl; //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 };
Also See more posts : www.comsciguide.blogspot.com
3. 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); // error
color red1 = static_cast<color>(11) // 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
Also See more posts : www.comsciguide.blogspot.com
4. 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.
Also See more posts : www.comsciguide.blogspot.com
5. 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
{
Also See more posts : www.comsciguide.blogspot.com
6. 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)
Also See more posts : www.comsciguide.blogspot.com
7. {
case RED : std::cout << "redn"; break;
case GREEN : std::cout << "greenn"; break;
case BLUE : std::cout << "bluen"; break;
}
return 0;
}
Also See more posts : www.comsciguide.blogspot.com