What is a struct variable in the form of a class definition

Updated on technology 2024-04-21
17 answers
  1. Anonymous users2024-02-08

    First, let's distinguish between subclasses and structs:

    There are only two differences between struct and class in C++:

    The default member access in is private, while in struct it is public.

    2.Inheritance from class defaults to private inheritance, while inheritance from struct defaults to public inheritance.

    Other than that, it makes no difference.

    As for structs in CPP, it also supports virtual, it supports inheritance, but I don't use it, so there's no need to mix the two concepts of structs and classes. Just add to your own annoyance, unless, you want to keep the style consistent with encapsulation, polymorphism, etc., in some part of the whole C-style project. In addition, it can be initialized in the form of curly braces.

    *Example 1***

    #include

    #include

    using namespace std;

    struct st

    st()a=0; b=0;

    st(int i,int j )

    a=i; b=j;

    cout<<" st init a="<

    struct test

    virtual v_proc()

    test()

    int operator +(const int a)

    struct test_ch : public test

    private:

    test_ch()

    virtual v_proc()

    test_ch()

    class ctest

    ctest()

    int main()

    test *o_test;

    o_test=new test;

    o_test->v_proc();

    std::cout <<"o_test->operator +(5)="delete o_test;

    test_ch *o_test_ch;

    o_test_ch=new test_ch;

    o_test_ch->v_proc();

    std::cout <<"o_test_ch->operator +(6)="delete o_test_ch;

    ctest *o_ctest;

    o_ctest=new ctest;

    delete o_ctest;

    return 0;

  2. Anonymous users2024-02-07

    It is to define a class that has only data members in it. In fact, classes are almost the same as structs when they only have public data members.

  3. Anonymous users2024-02-06

    Structs are frequently used data types that are used just as often as pointers, so they need to be taken seriously, but the usage is very simple.

    Definition of struct variables. The previous only defines the struct type named student, not a struct variable, just like int, just a type. Next, define a struct variable in a number of ways.

    1.Define the struct type first, then define the variablesRow 6 defines a struct variable, and the variable name is used in conjunction with student.

    2.Define the struct type at the same time as define the variable, and the struct variable is named stu.

    3.Directly define the struct type variable, omit the type name, and the struct variable is named stu.

    Fourth, the precautions of the structure.

    1.Recursive definitions of the struct itself are not allowed. The following is wrong, note line 3.

    2.A structure can contain other structures.

    3.Defining a struct type only describes what the type consists of, and does not allocate storage space to it, just as the system does not allocate space for the int type itself. Storage space is allocated to a variable only if it is a struct type is defined.

    Rows 1 and 4 do not allocate storage space, and the system will allocate storage space to the stu variable when line 6 is executed.

    4.The memory space occupied by struct variables is the sum of the memory occupied by their members, and the members are arranged in memory in a defined order. For example, the following student struct:

    In a 16-bit compiler environment, a student variable occupies a total of 2 + 2 + 4 = 8 bytes.

  4. Anonymous users2024-02-05

    The keyword struct is a struct type.

    per is the name of this struct type.

    per per ;per is a defined struct variable.

    12345678910111213141516171819202122typedef struct perper;In general, when defining a variable, you can struct per per; or directly per per; The definition method you write can only define variables with per per if you write struct per; Variables can only be defined in a struct per per per way.

  5. Anonymous users2024-02-04

    Structure type variables are different types of data variables that are combined into a whole, and although each variable belongs to a different data type, they are closely related to each other, and all kinds of information belong to the same person. In this case, you can declare a structure type, which consists of multiple variables. Variables can be basic or custom data types.

    The differences between struct type variables and struct member variables are as follows: different memory units, different compilations, and different assignments.

    First, the memory units are different.

    1. Structure type variable: The system allocates the actual memory unit for the structure type variable and stores specific data in it.

    2. Struct member variables: The struct member variable system does not allocate actual memory units for it.

    Second, the compilation is different.

    1. Structure type variables: At compile time, the system does not allocate space for the types of structure type variables.

    2. Struct member variables: At compile time, the system allocates space for the types of struct member variables.

    Third, the assignment is different.

    1. Structure type variables: Variables of different structure types are not allowed to assign values to each other, even if they have the same members.

    2. Struct member variables: The struct member variables are. Different structs are allowed to assign values to each other as long as they have the same members.

  6. Anonymous users2024-02-03

    A struct type variable is a struct type that you define and declare a variable of that type.

    The struct member variable is a member of the struct variable that you declare, and he is a variable variable.

    Case in point: struct bird

    It's a struct, and it's just a class, it's a way of organizing memory, it's incorporeal.

    Now use it to define a variable, bird a;

    This a is a struct variable, which is an object of a class that occupies a block of memory.

    Rather, it is a member variable in this variable, and once a is constructed, num exists.

    a:fly(), is a member function of this variable.

    That should be clear.

  7. Anonymous users2024-02-02

    Here's an example:

    struct a

    struct a x ;x is the struct type variable; is a struct member variable.

  8. Anonymous users2024-02-01

    Definition of structure type variables.

    Once a struct type is defined, the programmer can account for the memory variables of that struct type. It is described in the same form as the description of the variables for the simple data types described earlier. It is defined in the following format:

    Variable Name] Structure Name <[Field Value Table]>

  9. Anonymous users2024-01-31

    and normal variable types and pointers.

    It's the same. It's just that some functions only support passing pointers.

    Such as int type.

    There is a function. a(a*point)

    Your *tm now can be passed in.

    You can also pass other type pointers here, just turn it inside, and if you freeze the specific type, you can't turn it.

    b(atm_now_t)

  10. Anonymous users2024-01-30

    1.If you are using a C compiler (some of them), then the compiler may not support writing rec t1 directly, it should be written as struct rec t1

    2. typedef struct rec rec;This means that struct rec is defined as recAfter that, rec t1, t2 can be used; instead of struct rec t1,t2; (instead of adding t1, t2 directly after {} in the appeal).

    3. struct {}rec;No, the rec here is not the name of the struct, but an example. It's like int a; You can't write a b to define b.

    If you want to write it this way, then just write struct {}t1,t2; This way, you get two struct instances, but then you need to override struct{} when you want to define them. So, this is only used if you're sure you only need to define t1, t2.

  11. Anonymous users2024-01-29

    Structs defined by the typedef method used to define a type name for the struct should be used as follows:

    struct s str;

    t str;

    These two effects are the same, you can try it, I hope it helps you.

  12. Anonymous users2024-01-28

    1、struct rec

    rec t1,t2;modify struct rec t1,t2;

    Because not all compilers support that way of writing.

    2、typedef struct rect1,t2;

    rec t1,t2

    I don't know what you mean, but you can write it like this.

    typedef struct

    rec;rec t1,t2

    Indicates that the structure should be renamed to rec, otherwise structs (especially the C compiler) will need to be added when using this type

    3、struct

    rec;This way of compilation should be passed, but the rec is considered to be a variable, not a type, in short, you need to define a t1 or t2 variable, which is commonly written in the following ways: struct

    t1,t2;

    struct rec

    struct rec t1,t2;

    typedef struct

    rec;rec t1,t2;

    I hope you understand the principle.

  13. Anonymous users2024-01-27

    The keyword struct is a struct type.

    per is the name of this struct type.

    per per ;per is a defined struct variable.

    12345678910111213141516171819202122typedef struct perper;In general, when defining a variable, you can struct per per; or directly per per; The definition method you write can only define variables with per per if you write struct per; Variables can only be defined in a struct per per per way.

  14. Anonymous users2024-01-26

    typedef is equivalent to taking an alias.

    The phrase typedef struct per and the end per are both struct type names; (Unless the typedef is omitted but the end of the per has to be changed, because the modified per has become the struct variable name).

    In C, typedef is the custom variable name and struct is the struct keyword, so C is wrong.

  15. Anonymous users2024-01-25

    per is a struct variable, which has no name.

  16. Anonymous users2024-01-24

    How to enroll students for the training course? In the face of countless training institutions in the circle, how to stand out in the fierce enrollment? It only takes a few steps to get it done, making admissions easier and more efficient, and spending less money.

  17. Anonymous users2024-01-23

    Explanation: There are three possible scenarios for defining variables:

    1) Define at the beginning of the function:

    2) Defined in a compound statement within the function.

    3) Define the function externally.

    Variables defined inside a function are only valid within the scope of this function, that is, they can only be referenced within this function, and they cannot be used outside of this function. Variables defined in a compound statement are valid only within the scope of the compound statement, and they can only be referenced within the compound statement. These variables cannot be used outside of the compound statement.

    These are the things that become"Local variables".

    The compilation unit of a program is the source program file, and a source file can contain one or more functions. The variables defined inside the function are local variables, while the variables defined outside the function are called external variables, and the external variables are global variables (also known as full variables).Global variables can be shared by other functions in this file.

    It is valid from the point where the variable is defined to the end of the source file.

Related questions
4 answers2024-04-21

I've seen questions like this, and I don't understand what you mean, but I think it's like this, structab; I hit ... where you can add definitions of all data forms, e.g., int >>>More

6 answers2024-04-21

Legal analysis: the national law has made more provisions on the form of contract, and the state recognizes that the legal and effective contract form includes the written form of the contract, the oral form of the contract, and other forms of contract, among which the written form is more popular in daily life, and the written form includes the form of contract, fax, etc. >>>More

19 answers2024-04-21

The connection methods of steel structure include welding, ordinary bolt connection, high-strength bolt connection and riveting. >>>More

8 answers2024-04-21

The advantages of the cylinder structure: the main resistance to lateral force, the surrounding shear wall encloses a vertical thin-walled cylinder and a column frame to form a vertical box-shaped section of the frame tube, forming a whole, and the overall action is load-resistant. >>>More

6 answers2024-04-21

What are the common chromosomal structural abnormalities? The basis of chromosomal structural abnormalities is chromosomal breakage. Experts point out that if the break heals immediately, no abnormality occurs, and if the fracture is recombined, it will lead to chromosomal abnormalities, so what are the common chromosomal structural abnormalities? >>>More