Some basic data structures for C + +: Literal constants, symbolic constants, enumeration constants

Source: Internet
Author: User

1. Constants: C + + consists of two constants, literal constants and symbolic constants.

literal constants: refers to the values entered directly into the program, such as: in myage=26; myAge is an int type variable, and 26 is a literal constant.

Symbolic constants: refers to constants represented by names, as if they were variables, but once initialized, their values cannot be changed.

The main point is: ①const defined constants: const unsigned short int studentperclass=15; here studentperclass is a symbolic constant

② constants defined with # include: #define STUDENTPERCLASS 15 Here's studentperclass is also a symbolic variable

2. Enumeration constants:

Enumeration constants You can create a new type, and then define a new type variable that defines the values of those variables as a set of possible values.

Definition method: Enum COLOR {red,blue,green,white,yellow};

This statement has two changes after execution: A, color becomes the name of the enumeration, and becomes a new type

b, if there is no special description, the content of the identifier in the default enumeration is the shaping constant that increments from 0, that is, red=0,blue=1,green=2,white=3,yellow=4

Of course, you can define the value of the enumeration constant yourself:

Enum COLOR {red=100,blue=200,green=300,white=400,yellow=500};

Examples:

#include <iostream>#include<iomanip>using namespacestd;intMain () {enumDays {Monday,tuesday,wednesday,thursday,friday,saturday,sundy};    Days D1; D1=Monday;
D1=0, so the assignment is wrong, only the enumeration value can be assigned to the enumeration variable, the value of the element can not be directly assigned to the enumeration variable.
If you have to assign the value to the enumeration variable, you must use the coercion type conversion, for example here: d1= (enum days) 0; The meaning is to assign the enumeration element of sequence number 0 to the enumeration variable A, which is equivalent to: a=monday; cout<<d1<<Endl; System ("PAUSE"); return 0;}

Run the program and the final print result is "0" instead of "Monday"

A few notes about enumerations:

The ① enumeration value is a constant, not a variable. You cannot assign a value to an assignment statement in a program. For example, the elements of the enumeration days are then assigned the following values: Monday=1; The tuesday=10 are all wrong.

② can only assign enumeration values to enumeration variables, and cannot assign the value of an element directly to an enumeration variable.

Some basic data structures for C + +: Literal constants, symbolic constants, enumeration constants

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.