Sample Code for C ++ enum Enumeration

Source: Internet
Author: User

The use of enum enumeration in C ++ programming language is hard to grasp. Today, we have provided four simple code examples to help you understand the specific meaning and application methods of the C ++ enum enumeration, so as to facilitate your practical application in the future.

  • How to parse the C ++ operation clipboard
  • Detailed Implementation of C ++ string replacement Functions
  • Introduction to multiple implementation techniques of the C ++ Singleton Mode
  • What is the difference between the C ++ pointer and the reference?
  • How to Implement C ++ file stream operations

C ++ enum enumeration format:

Enum <Enumeration type name >{< enumeration table >}; (see example 1)

Enum {<enumeration table >}< variable table>; (see example 2)

The first C ++ enum enumeration value corresponds to an integer. Generally, the first enumeration value corresponds to a constant value of 0, which is then low. (special usage Example 3 Example 4)

Enum bool {false, true}; bool type is C ++ pre-defined Enumeration

Example code of C ++ enum enumeration:

 
  1. # Include <iostream. h>
  2.  
  3. Void main ()
  4. ...{
  5. // Example 1
  6. Enum day... {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; // defines the enumeration of a day
  7. Day d = Sun;
    // D can only be equal to one of "Sun, Mon, Tue, Wed, Thu, Fri, Sat". There is no other value!
  8. Cout <"value is" <d <endl;
  9.  
  10. // You can also define multiple variables.
  11. Enum day... {Sun, Mon, Tue, Wed, Thu, Fri, Sat };
  12. Day d1, d2, d3;
  13. D1 = Thu; d2 = Sat; d3 = Tue;
  14. Cout <"d1 & d2 & d3" <d1 <"" <d2 <"" <d3 <endl;
  15.  
  16. // Example 2
  17. Enum... {Sun, Mon, Tue, Wed, Thu, Fri, Sat} d;
  18. D = Mon;
  19. Cout <"value is" <d <endl;
  20.  
  21. // Example 3
  22. Enum... {Sun = 10, Mon, 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 10 11 12 13
  25.  
  26. // Example 4
  27. Enum... {Sun = 10, Mon = 1, Tue, Wed, Thu, Fri, Sat} d1, d2, d3, d4;
  28. D1 = Sun; d2 = Mon; d3 = Tue; d4 = Wed;
  29. Cout <d1 <"" <d2 <"" <d3 <"" <d4 <endl;
    // Output 10 1 2 3
  30. }

The above is an introduction to the C ++ enum enumeration.

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.