Friday, 4 August 2017

How to use enumeration in structure in C/C++

How to use enumeration in structure in C/C++ .

What is enumeration ?

An enumeration is a complete, ordered listing of all the items in a set. The term is typically used in arithmetic and computer science to consult a list of all the elements of a set. The specific requirements for an enumeration (for example, whether the set must be finite, or whether the list is allowed to include repetitions) rely upon the field of look at and the context of a given problem.

Short for enumeration, an enumeration variable kind may be discovered in C (ANSI, not the authentic K&R), C++ and C#. The idea is that instead of the usage of an int to represent a fixed of values, a kind with a restrained set of values in used as a substitute.

Declaration Of Enumeration:

An enumeration is a distinct type whose value is limited to a number of values (see below for details), which may include several explicitly named constants ("enumerators"). The values of the constants are values of an integral type known as the underlying form of the enumeration.

An enumeration is defined with the aid of enum-specifier, which appears in decl-specifier-seq of the statement syntax. The enum-specifier has the subsequent syntax:

1 - enum-key attr(optional) identifier(optional) enum-base(optional) { enumerator-list(optional) }
2 - enum-key attr(optional) nested-name-specifier(optional) identifier enum-base(optional) ;


Example Code:

// dayenum.cpp
// demonstrates enum types
#include <iostream>
using namespace std;
//specify enum type
enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
int main()
{
days_of_week day1, day2; //define variables
//of type days_of_week

day1 = Mon; //give values to
day2 = Thu; //variables
int diff = day2 - day1; //can do integer arithmetic
cout << "Days between = " << diff << endl;
if(day1 < day2) //can do comparisons
cout << "day1 comes before day2\n";
return 0;
}

Output:

 













That's probably what I want:

 Perhaps. Just understand that the values for the enums are still pretty tons worldwide (over the compilation unit), even if the enum sorts are nested — and even while they're anonymous. You cannot use the same identifier in special enums, if they're each visible.My advice would be to keep declaring your sorts at the worldwide scope, however make certain your column name identifiers denote which keep they are being carried out to.Also, hold your compilation devices small. You should absolutely be placing the code for every of your application's widgets in separate source files. This limits the scope of such "international" values.

Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.




Viral Programs

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment