Custom data type C + + struct type community type Enum type class type {}

Source: Internet
Author: User

One. struct type

struct types, common body types, enumeration types, class types, and so on are collectively referred to as custom types (User-defined-type,udt).

Structs are equivalent to records in other high-level languages (record), for example:

struct student{

int num;

Char name[20];

char sex;

int Agel

Float score;

Char addr[30];

};

The general form of a struct declaration:

struct struct type name {member List};

The struct type name is used as a flag for the struct type, and the student is the struct type name. In the example above, Num,name is called a member of the struct. When declaring a struct type, you must make a type declaration for each member, that is, the type name member name, and each member is called a domain in the struct, and the member list is also known as the domain table .

Note: In C, the members of a struct can only be data (as in the example above); This is augmented in C + +, where members of a struct can include data and can include functions (member functions) to accommodate object-oriented programming.

Two. Method and initialization of struct type definition

1. There are three ways to define struct type variables:

(1) Declare the struct type first, and then define the variable name.

If a struct type studenthas been defined above, it can be used to define struct-body variables . Such as

Student Student1,student2;

After the structure variable is defined, the system assigns an inner deposit element.

In C, when defining struct variables, the use of C is preserved by adding the keyword struct,c++ to the struct type name. such as: struct Student student1,student2;

(2) When declaring a variable, define it at the same time.

struct Student

{

int num;

Char name[20];

int sex;

Float score;

Char addr[30];

}student1,student2;

The form of this definition is:

struct Structure Body name

{

Member List

} variable list;

(3) Directly define the structure of the variable type, the general form of the period:

struct

{

Member List

} variable Name list;

remember: At compile time, you will not allocate space for the type, just allocate space for the variable. The member name in the struct can be the same as the variable name in the program, but the two are not necessarily associated. For example, in a program you can define an integer variable num, which is different from the NUM in student and does not affect each other.

2. Initialization of struct variables.

struct Student

{

int num;

Char name[20];

char sex;

int age;

Float score;

Char addr[30];

}student1={10001, "Wangjiun", "M", 19,90.5, "Sahgnhai"};

You can also separate the declaration and definition types and initialize them when defining variables. Such as

Student student1={10002, "Chengong", ' f ', 20,98, "Beijing"};//student are declared struct types;

3. References to struct variables.

General way of referencing members in struct variables: struct variable name. member name; For example, student1.num=10001;

"." is a member operator . It has the highest precedence among all operators. Student1.num can therefore be viewed as a whole.

4. Structure array

struct arrays differ from numeric arrays: Each array element is a struct type of data, and they all include individual member items .

Define struct arrays: similar to defining struct-body variables, you only need to declare them as arrays when defining a struct-body array.

struct Student

{

int num;

Char name[20];

int age;

Float score;

Char addr[30];

};

Student stu[3];//defines an array of Student types Stu

You can also define a struct array directly. Such as:

struct Student

{

int num;

Char name[20];

char sex;

int age;

Float score;

Char addr[30];

}STU[3];

Or:

struct

{

int num;

Char name[20];

char sex;

int age;

Float score;

Char addr[30];

}STU[3];

struct initialization: struct initialization As with other arrays, the struct array can be initialized.

struct Student

{

int num;

Char name[20];

char sex;

int age;

Float score;

Char addr[30];

}stu[3]={{1001, "Linlin", ' M ', 18,98.4, ' Beijing '},{1002, ' Lijun ', ' F ', 18,98.4, ' Beijing '},{1001, ' Wangxiajian ', ' m ', 18,90, "Shanghai"};

When you define an array Stu, you can also specify no number of elements, which can be written as:

Stu[]={{...},{...},{.}}; At compile time, the system determines the number of elements in the array by the number of structure constants of the initial value of the root play.

Three. Pointer to struct variable

A pointer to a struct variable is the starting address of the memory segment that the variable occupies.

You can set a pointer variable to point to a struct variable, at which point the value of the pointer variable is the starting address of the struct variable. Pointer variables can also be used to point to elements in a struct array.

1. Reference a member in a struct variable by a pointer to a struct variable .

#include <iostream>

#include <string>

       using namespace std;
       struct Gjjou
        {
                int num;
                string name;
                char sex;
                float score;
       };
       int main ()
       {

                Gjjou Stu;
                Gjjou *p=&stu;
                stu.num=103001;
                stu.name= "wangjingjing";
                stu.sex= ' F ';
                stu.score=9.9;
                cout<< (*p) .num<< "" << (*p) .name<< "" & lt;< (*p) .sex<< "" << (*p) .score<< "" <<endl;
                cout<<stu.name<< "" <<stu.num<< " <<stu.sex<< "<<stu.score<<" <<endl;
                return 0;
         }

For intuitive use, C + + provides a pointer to the struct variable, for example, P->num represents the member num in the struct variable that the pointer P is currently pointing to. P-> and (*p). Num are equivalent. The following three forms are equivalent:

1. struct. member name. For example, Stu.num;

2. (*p). Member name. For example, (*p). Num;

The 3.p-> member name. such as P->num. "," is called a pointer operator.

Note:

P->n gets the value of member n in the struct variable that p points to.

P->n++ gets the value of member n in the struct variable that the P points to, which is used to add 1 to the value.

++p->n gets the value of member n in the struct variable that p points to and adds 1 to it before using it.

2. Construct a linked list with a struct variable and a pointer to a struct variable.

3. struct type data as function parameters.

(1) A pointer to a struct variable is used as an argument to pass the address of the struct variable to the formal parameter .

(2) The reference variable of the struct variable as the function parameter.

Four. operator new and delete for dynamically allocating and revoking memory

1. For inserting and deleting nodes in a dynamic list, C uses the library function malloc and free to allocate and revoke memory space. C + + provides operator new and delete to replace malloc and delete.

Note: New and delete are operators. is not a function.

New int;//opens up a storage space and returns an address to that space.

new int (100);//opens a space for storing integers and specifies that the integer has an initial value of 100 and returns an address to that storage space

New CHAR[10]; Opens a space for storing character arrays (including 10 elements) and returns the first address.

New Int[5][4]; Opens a space that holds a two-dimensional integer array (size 5*4), returning the address of the first element.

Float *p=new float (3.141592); Open a space for storing single precision, and specify that the real number of the initial value is 3.141593, the returned space address is assigned to the pointer variable p;

The general format used by the new operator:

new type [initial value];

Note: You cannot specify an initial value when allocating array space with new . If space is not allocated properly due to insufficient memory, new returns a null pointer null, and the user can determine whether the allocated space succeeds based on the value of the pointer.

Delete [] pointer variable

Before revoking the space of the single precision opened by new, the delete p should be used;

"New char[10" in front; Open character array space, if you assign a pointer to a pointer variable PT, you should undo the space in the following form: delete [] pt;

Custom data type C + + struct type community type Enum type class type {}

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.