Explain the types of enumerations in C + + and how to declare new types _c language

Source: Internet
Author: User
Tags constant

C + + Enumeration type
If a variable has only a few possible values, it can be defined as an enumeration (enumeration) type. The so-called "enumeration" refers to the value of the variable enumerated, the value of the variable can only be listed in the range of values. Declaring an enumeration type begins with an enum. For example:

  Enum Weekday{sun, Mon, Tue, Wed, Thu, Fri, Sat};

The above declares an enumerated type weekday, in curly braces Sun, Mon, ..., Sat, etc. is called an enumeration element or an enumerated constant. The value of a variable representing this type can only be one of the above 7 values. They are identifiers that the user defines themselves.

The general form of declaring an enumeration type is:

  Enum Enum type name {Enum constant table column};

After declaring an enumeration type, you can use it to define a variable. Such as:

  Weekday workday,week_end;

In this way, workday and week_end are defined as variables of the enumeration type weekday.

In the C language, the enumeration type name includes the keyword enum, and the above definition can be written as:

  Enum weekday workday,week_end;

In C + +, you are allowed to write an enum, and you do not write an enum, but you keep the use of C. Based on the above declaration of the enumerated type weekday, the value of the enumeration variable can only be one sun to the SAT. For example:

  Workday=mon; Week_end=sun;

is correct. You can also define enumerated variables directly, such as:

  Enum{sun, Mon, Tue, Wed, Thu, Fri, Sat} workday,week_end;

These identifiers do not automatically represent what they mean.

Several descriptions of the enumeration types:
Enumeration constants are referred to as constant processing.
Enumeration elements are constants, they are valued, and C + + compilation assigns them to 0,1,2,3 in the order in which they are defined,...。 You can also specify the value of an enumeration element when declaring an enumeration type.
Enumeration values can be used to make judgment comparisons.
An integer cannot be assigned directly to an enumeration variable.

"Example" pockets of red, yellow, blue, white, black 5 colors of several balls. Every time you take 3 balls out of your pocket, ask for the possibility of 3 different colors of the ball, output each arrangement.

#include <iostream> #include <iomanip>//use namespace STD for SETW controller in the output; int main () {enum color {red,yellow,blue,white,black};//declare enum type color color pri;//define variable pri int I,j,k,n=0,loo of color type P n is the cumulative number of combinations of different colors for (i=red;i<=black;i++)//When I is a certain color for (j=red;j<=black;j++)//When J is a certain color if (I!=J)//If the first two balls of Yan
        Color different {for (k=red;k<=black;k++)///Only the first two balls have different colors, you need to check the color of the 3rd ball if ((k!=i) && (K!=J))//3 balls have different colors {n=n+1;//makes the cumulative value N plus 1 cout<<setw (3) <<n;//Output the current N value, the field width is 3 for (loop=1;loop<=3 loop++)///3 balls processed {switch (loop)//loop value successively is 1,2,3 {case 1:pri=color (i); b Reak; Color (i) is a mandatory type conversion, so that the value of the PRI is I case 2:pri=color (j); The value of the PRI is J case 3:pri=color (k);
      The value of the PRI is K default:break;
        Switch (PRI)//To judge the value of the PRI, output the corresponding "color" {case RED:COUT&LT;&LT;SETW (8) << "Red"; Case YeLLOW:COUT&LT;&LT;SETW (8) << "Yellow"; Case BLUE:COUT&LT;&LT;SETW (8) << "Blue";
        break; Case WHITE:COUT&LT;&LT;SETW (8) << "White";
        break; Case BLACK:COUT&LT;&LT;SETW (8) << "BLACK";
        break;
      Default:break;
   }} cout<<endl; } cout<< "Total:" <<n<<endl;
The number of output eligible combinations return 0;
 }

The results of the operation are as follows:

1 red Yellow Blue 2 red yellow white 3 red yellow black ┆ ┆ ┆-black white red-white
Yellow Black white
Blue
total:60

Instead of enumerating constants, using constant 0 to represent "red" and 1 for "yellow" ... It's OK. However, it is obvious that enumeration variables are more intuitive, because the enumeration elements are chosen with a "known meaning" identifier, and the value of the enumeration variable limits the range of enumerated elements that are specified when it is defined. If you give it a different value, you get an error message that makes it easier to check.

C + + typedef declares new type
in C + +, in addition to declaring a struct, a common body, an enumeration, and other types, you can declare a new type name in a typedef instead of an existing type, such as:

    • typedef int INTEGER; Specifies an int type with an identifier integer
    • typedef float REAL; Specify the float type with real

In this way, the following two lines are equivalent:

    • int i,j; float a,b;
    • INTEGER i,j; Real A,b;

This allows people who are familiar with Fortran to define variables with integers and real to suit their habits.

If an integer variable is specifically used in a program to count, you can use count as the integer type name:

  typedef int COUNT; Specifies to use count to represent the int type
  count i,j;//define variable I,j as Count type

That is, the int type defines the variable i, j as the count type in the program, and makes it more obvious to people that they are used for counting.

You can also declare the structure body type:

typedef struct//Note the keyword typedef was used before struct, which means declaring the new name
{
  int month; int day;
DATE; Note that date is a new type name, not a struct variable name

The declared new type name date represents a struct type specified above. This allows you to define variables with date:
DATE birthday;  DATE *p; P is a pointer to data for this struct type
can also be further:

typedef int NUM[100]; Declares that num is an integer array type, contains 100 elements
NUM N;//defines n as an array of 100 integer elements
typedef char *string;//declare string to character pointer type
STRING p,s[ 10]; P is a character pointer variable, s is an array of pointers (10 elements)
typedef int (*pointer)//Declaration pointer as a pointer to a function, the function returns an integer value
pointer p1, p2;//P1, Pointer variable p2 to pointer type

To sum up, the method of declaring a new type name is:

    • The definition statement (such as int i;) is written first by defining the variable.
    • Change the variable name to a new type name (for example, replace I with count).
    • Add a typedef (such as typedef int COUNT) to the front.
    • You can then define the variable with the new type name.

For example, declare the array type above:

    • Write first by definition array form: int n[100];
    • Change the variable name n to its own specified type name: int num[100];
    • In front plus a typedef, get the typedef int NUM[100];
    • Used to define a variable: NUM n; (n is an array of 100 integer elements).

It is customary to represent the type names declared with typedef in uppercase letters in order to differentiate them from the standard type identifiers provided by the system.

A few notes on the typedef:

    • A typedef can declare various types of names, but it cannot be used to define variables. It is convenient to use TypeDef to declare array type and string type.
    • Using typedef simply adds a type name to a type that already exists without creating a new type.
    • When you use the same type of data in different source files (especially arrays, pointers, structs, common bodies, and other types of data, a common typedef declares some data types, puts them in a single header file, and then includes them in a file that needs to be used with the #include command. To improve programming efficiency.
    • Using typedef is beneficial to general and porting programs. Sometimes the program relies on hardware features and is easy to migrate with a typedef.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.