How enum is used in C + + (GO)

Source: Internet
Author: User

Enum overview

Enum enum type is a type of data in C/s + +, which is a user-defined type like struct and class, which is characterized by the fact that the value of the enum type variable is limited and can be enumerated.

Enum definition

C + + Enum enumeration format:

Enum < enum type name > {< enumeration Table >};

Enum {< Enumeration table >}< variable Name table >;

The first C + + Enum enumeration value corresponds to an integer number, usually if the enumeration constant does not define a numeric value, then the first enumeration value corresponds to the constant value 0, and then increments, and if the first enumeration constant defines a value, then the value is incremented, with each constant separated by "," instead of ";", the last value does not use a symbol.

enum BOOL {false,true}; The bool type is a pre-defined enumeration of C + +

enum Example

  1. #include < iostream.h>
  2. void Main ()
  3. ... {
  4. Example 1
  5. Enum Day ... {sun,mon,tue,wed,thu,fri,sat};//defines an enumeration of day
  6. Day d = Sun;
    D can only be equal to one of "Sun,mon,tue,wed,thu,fri,sat", no other value!
  7. cout< < "value is"< < D<   < Endl;
  8. You can also define multiple variables
  9. Enum Day ... {Sun,mon,tue,wed,thu,fri,sat};
  10. Day d1,d2,d3;
  11. D1 = Thu; d2 = Sat;  d3 = Tue;
  12. cout <   <   "d1 && d2 && d3 " <   <   d1 <   <   " " <   <   d2<   <   " " <   <   d3<   <   Endl ;  
  13. Example 2
  14. Enum... {Sun,mon,tue,wed,thu,fri,sat} D;
  15. D = Mon;
  16. cout< < "value is"< < D<   < Endl;
  17. Example 3
  18. Enum...  {sun=10,mon,tue,wed,thu,fri,sat} d1,d2,d3,d4;
  19. D1 = Sun; d2 = Mon; d3 = Tue;  d4 = Wed;
  20. cout<<D1< < "< < D2< < " "<  < D3<   < ""< < D4< < Endl;
    Output ten
  21. Example 4
  22. Enum...  {sun=,mon=1,tue,wed,thu,fri,sat} d1,d2,d3,d4;
  23. D1 = Sun; d2 = Mon; d3 = Tue;  d4 = Wed;
  24. cout<<D1< < "< < D2< < " "<  < D3<   < ""< < D4< < Endl;
    Output 1 2 3
  25. }

How enum is used in C + + (GO)

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.