c++--Study notes 007

Source: Internet
Author: User

20150430

Enumeration type If there is no assignment, there is a default integer value, which in turn is 0,1,2,3 ...

For example: The value of Weekday::mon is 0

We can define a value ourselves.

Re-specify the value of the enumeration element

Enum Weekday

{

Mon=1,

Tue

Wed

Thu

Fri

Sat

Sun=0

};

Because of the particularity of the Weekday::sun, it is specified as 0

Assigning values to enumerated type variables

Defining a variable of an enumeration type

Weekday Nday

Nday=weekday::sun;

Attempting to assign a value to an enumeration type variable with a different value produces a compilation error

nday=7;

Weekday::mon= (weekday) 0;//attempt to change the value of an enumeration element, resulting in a compilation error

Complex things are described by struct type

A struct is a combination of the basic data types that describe a single attribute to describe something more complex, forming a new stereotype data type.

Syntax format for structs

struct Structure Body name

{

Data type 1 member name 1;

Data type 2 member name 2;

..

Data type N member name N;

};

Within a struct, variables of different data types are used to represent the various properties of a complex object, respectively.

Define structure body human describe the complex food of man

struct Human

{

String M_strname; Name

BOOL M_bmale; Gender

int M_nage://Age

int m_nheight; Height

float M_fweight; Weight

};

Syntax format for referencing struct member variables:

struct variables. member variables

Using the "" operator

A struct variable represents a variable defined with a struct body.

By referencing a struct member variable, you can read or set the value of each member variable of a struct to complete a description of the complex thing.

Assigning values to member variables

Chenliangqiao.m_strname= "Chenliangqiao";

Chenliangqiao.m_bmale=true;

Read the value of a member variable

int nage=chenliangqiao.m_nage;

struct Emplyee

{

String M_strname;

BOOL M_bmale;

int m_nage;

int m_nsalary;

};

const int max_count=1000;

int _tmain (int argc,_tchar* argv[])

{

Create a struct array to manage multiple struct variables

Emplyee Arremplyee[max_count];

cout<< "Please enter employee Information" <<endl;

int ncount=0;

Do

Receive user input

Saving user-entered data to individual member variables of a struct variable

Cin.clear ();

Cin>>arremplyee[ncount].m_strnmae

>>arremplyee[ncount].m_bmale

>>arremplyee[ncount].m_nage

>>arrEmplyee[ncount].m_nSalary;

Check that the input is complete

if ("End" ==arremplyee[ncount].m_strname)

Break

Start entering the next employee information

++ncount;

}while (ncount <max_count);

Show information for all employees

cout<< "Employee information: \ n name \ t gender \ t age \ t Salary" <<endl;

for (int i=0;i<ncount;++i)

{

Accessing member variables of struct variables in an array

Get the various properties of the "Employee" complex transaction

cout <<arrEmplyee[i].m_strName<< "\ T"

<<arrEmplyee[i].m_bMale<< "\ T"

<<arrEmplyee[i].m_nAge<< "\ T"

<<arrEmplyee[i].m_nSalary<<endl;

}

return 0;

}

\ t input format horizontal Skip

Pointer

By direct access to the memory of the data volume, you can play a 42-ton effect.

When the amount of data accessed is large, use the pointer

Improper use of pointers, very bad

The pointer is the data type that represents the memory address

Using a pointer, the program can manipulate the memory directly through the pointer.

Two ways of accessing memory:

1. Access data stored in memory through variable name introduction, read or write

2. The second way is to access the data in memory directly from the pointer.

The memory address stored in the pointer may be the address of a function, an object address, an integer address.

If the pointer variable holds an object or the address of a function, it is said that the pointer points to the object or function.

Defining pointer variable syntax

data type * variable name ;

The data type represents what data type the pointer is pointing to, which can be int,string,double, etc.

The variable name is the name of the pointer.

* Represents a variable that declares a pointer type

Data types are also known as pointer types

A pointer to an integral type of data is called an integer pointer

Coding rules: One statement only accomplishes a single thing.


c++--Study notes 007

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.