Process oriented-Object oriented (c + + encapsulation, this) _ Memory four-zone _ Variable life cycle

Source: Internet
Author: User

1, object-oriented mainly involves constructors, destructors, virtual functions, inheritance, polymorphism and so on.

2. The underlying implementation mechanism for various support

In C, data and manipulation of data (functions) are declared separately , that is, the language itself does not support the association of "Data and functions".

In C + +, data and functions are defined directly in the class by using abstract data type, ADT. .

C + + member data: static, nonstatic

C + + member functions: Static, nonstatic, virtual

class classes in C + +, starting from Object-oriented theory, will variable (property) and the function (method) defined together to describe classes in the real world; but from a computer perspective, the program is still composed of data segments and code snippets.

How does the C + + compiler accomplish the transformation of object-oriented theory to computer programs ? How does C + + manage the relationships between classes, objects, and both?

When an object calls a class, how does the C + + compiler differentiate which method is called by which object?

Look at a chestnut:

#include"iostream"using namespacestd;classc1{ Public:         inti; intJ; intK; protected:  Private:  };classc2{ Public:        inti; intJ; intK; Static intm;  Public:         intGETK ()Const{returnK;} voidSETK (ITN val) {k =Val;} protected:  Private:  }    structs1{inti; intJ; intK; }structs2{inti; intJ; intK; Static intm; }intMain () {printf ("C1:%d \ n",sizeof(C1)); printf ("C2:%d \ n",sizeof(C2)); printf ("S1:%d \ n",sizeof(S1)); printf ("S2:%d \ n",sizeof(S2));}

4 values are 12, conclusion:

Attributes (normal member variables) are placed in the stack;

static properties (variable) are placed in the global data area;

The method (member function) is placed in the code area.

Use the this pointer to distinguish which object is calling the common method, and who calls this common method, to pass whose address to the this pointer.

C + + compiler internal processing of ordinary member functions: procedure-oriented implementation of adding this pointer

Look at a chestnut:

Classes for C + +:

classtest{Private:        intM1; Public: Test (inti) {M1-i; }         intGet1 () {returnM1; }         Static voidThe Print ()///static member function belongs to the entire class, does not belong to a variable, and there is no this pointer {printf ("This is class Test. \ n ");         }}; Test A (Ten); A.get1 (); Test::P rint ();

The corresponding process-oriented implementation process.

structtest{intM1;};voidTest_initialize (Test *pthis,inti) {pThis->M1-i; }intTest_get1 (Test *pthis) {returnPthis->M1; }voidTest_print () {printf ("This is class Test. \ n"); }}; Test A; Test_initialize (&a,Ten); Test_get1 (&a); Test_print ();

Conclusion:

1. The member variables and member functions of the C + + class object are stored separately . The Memory 4 area model in C language is still valid;

2. The normal member function of a class in C + + implicitly contains a pointer to the current object .

3, static member functions, static member variables belong to the class. A static member function does not contain a pointer to a specific object, and the normal member function contains a pointer to a specific object.

For a four-zone memory model:

Memory four-zone model:

Process description
1. The operating system load the physical hard disk code into memory
2. The operating system divides the C code into four zones
3. The operating system found the main function entrance execution

Char*fa () {Char*PA ="123456";//The pa pointer is in the stack area, "123456" is in the constant area, and the pointer variable PA is freed after the function is called. Char*p = NULL;//pointer variable p allocates 4 bytes in the stackP= (Char*)malloc( -);//This function opens up the memory space of a heap area and assigns the address to Pstrcpy (P,"Wudunxiong 1234566");//Copy the string from the constant area to the heap area returnP//return to the main function fb (), relative to FA, FB is the keynote function, and relative to the FA (), the FB () is called function}Char*fb () {Char*pstr =NULL; Pstr=fa ();returnPstr//pointer variable pstr It's over here.}voidMain () {Char*str =NULL; Str=FB (); printf ("str =%s\n", str);  Free(str);//to prevent memory leaks, the stored value of the memory allocated by the function FA () is passed to the main function by the return value, and then the main function frees the memorystr = NULL;//prevents the creation of wild pointersSystem"Pause");}

Summarize:

1. The memory space (heap, stack, global zone) allocated by the main function can be used in the called function, and can be used in the form of pointers as function parameters;

2. The memory space allocated by the called function is only the heap area (the memory area pointed to by the pointer) and the global zone (static variables and constants) can be used in the main function (return values and function parameters), and The stack area (the address where the pointer is stored) does not work, because the memory compiler used by this function automatically frees you after the stack function body has run out of space.

Process oriented-Object oriented (c + + encapsulation, this) _ Memory four-zone _ Variable life cycle

Related Article

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.