ENUM - Make U R Names As Data Types
ENUM - Make U R Names As Data Types
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 };
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/
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
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
// n == 21
cout<<col;
// output =0;
// compilation error
// compilation error
// error
// no error
// compilation error
// no error
// compilation error
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 example
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
{
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()
{
{
case RED
}
return 0;
}