Academic leave period Item 3 (1) Apply enumeration

Source: Internet
Author: User

Questions and codes:

Read Section 7.3 To learn about the general usage of enumeration types. Read the procedure below to output Mr. He buys a car solution, understanding the meaning of using enumerated types.

/**copyright (c) 2014, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: Mr. He buys a car plan. cpp* Author: Lenkidong * Completion date: February 26, 2015 * version number: v1.0*/#include <iostream>using namespace std;enum Color {red,black,white};enum Brand {  Lavida,tiggo,skoda};int Main () {int color,brand; For (color=red, color<=white; color++) for (Brand=lavida; brand<=skoda; brand++) if (! ( (Color==red&&brand==tiggo) | |      (Color==white&&brand==skoda)))          {switch (color) {case red:cout<< "red";        Break          Case black:cout<< "BLACK";        Break          Case white:cout<< "White";        Break          } switch (brand) {case lavida:cout<< "Lavida" <<endl;        Break          Case tiggo:cout<< "Tiggo" <<endl;        Break          Case skoda:cout<< "Skoda" <<endl;        Break }} return 0;} 

Operation Result:


Summary of Knowledge points:

The enumeration type has the following provisions in use: 1. The enumeration value is a constant, not a variable. You cannot assign a value to an assignment statement in a program. 2. The enumeration element itself is defined by the system as a numeric value representing the ordinal, from 0 to the beginning of the order defined as 0,1,2 .... As in weekday, the Sun value is 0,mon value of 1,sat value is 6. You can only assign enumeration values to enumeration variables, and you cannot assign the value of an element directly to an enumeration variable. If you must assign a value to an enumeration variable, you must cast with a coercion type. Such as:?
1 a=(enumweekday)2;
Special note: In compilers such as VS2010, the cast format should be: (weekday) 2, and if an enum is preceded, a "type redefinition" error occurs at compile time. The implication is that the enumeration element with sequence number 2 is given an enumeration variable A, which is equivalent to:?
1 a=tue;
It should also be explained that the enumeration elements are not character constants or string constants, and do not use single or double quotes.?
1234567 output result: 1 monday 2 tuesday 3 wendsday 4 thursday 5  friday 6 saturday 7 sunday 8 monday 9  tuesday 10 wendsday 11 thursday 12 friday  13 saturday 14 sunday 15 monday 16 tuesday  17 wendsday 18 thursday 19 friday 20 saturday 21 sunday 22 monday 23 tuesday 24 wendsday  25 thursday 26 friday 27 saturday 28 sunday  29 monday 30 tuesday
3. If the enumeration value conflicts with some keywords, for example:?
1 enumcourse{begin,then,end};
Since it is possible for a program to use iostream, and the namespace STD is used, it is possible to have an "ambiguous" sign error if you use end to assign a value directly. To resolve this problem, there are two methods: (1) You should avoid using common keywords when defining enumeration values, and you can prefix to differentiate for example:?
1 enumcourse{e_begin,e_then,e_end};
(2) The enumeration type can be added before the enumeration value: Course::end

Learning experience:

Good study Day Day up




Academic leave period Item 3 (1) Apply 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.