[C++] Graphical classes and objects (Part 1)

Classes and Objects (Part 1)



1. Preliminary understanding of process-oriented and object-oriented

The biggest difference between C language and C++ is that C language is process-oriented, focusing on the process, analyzing the steps to solve the problem, and gradually solving the problem through function calls. C++ is based on object-oriented and focuses on objects. It splits one thing into different objects and relies on the interaction between objects to complete the task. (Note: C++ is not purely object-oriented, Java is purely object-oriented. Since C++ needs to be compatible with C language, C++ can be mixed with object-oriented and procedural languages )


Insert image description here


2. Introduction of classes

C++ is compatible with C language, so it is compatible with struct structure syntax. C++ also upgraded struct to class


3. Class definition

1. If a small function wants to be inline, it can be defined directly in the class; 2. If it is a large function, it should bedeclared and defined separately

Insert image description here


4. Class access qualifiers and encapsulation

1. Access qualifier

Access qualifier description:

  1. Public modified members can be directly accessed outside the class,can also be accessed inside the class
  2. Protected and private modified members cannot be directly accessed outside the class (protected and private here are similar), can only be accessed inside the class (protected and private will be discussed later) There will be no difference until inheritance)
  3. The access scope begins at the occurrence of this access qualifier and ends at the occurrence of the next access qualifier.
  4. The default access rights of class are private and struct is public (because struct must be compatible with C)
    Note: Access qualifiers are only useful during compilation. When the data is mapped to memory, There is no difference in access qualifiers

2. Encapsulation

Encapsulation: organically combine data and methods of operating data, hide (member variables private/protected) the properties and implementation details of the object , onlyexposes the interface (open member function public) to interact with the object. Encapsulation is essentially a kind of management~
Insert image description here


5. Scope of class

The class defines a new scope, and all members of the class are within the scope of the class. When defining members outside a class, you need to use the ::
scope operator to indicate which class scope the member belongs to.
Insert image description here


Opening space in a class is a definition, and not opening a space is a statement; the so-called class is equivalent to a drawing, and space is opened only when the object is instantiated.
Insert image description here


6. Instantiation of classes

The process of creating an object using a class type is called instantiation of the class. The class is just a design drawing, and the object instantiated by the class is the house built on the drawing!
Insert image description here


7. Class object model

The storage method of class objects: only member variables are saved, and member functions are stored in the public code section.
Insert image description here


Conclusion: The size of a class is actually the sum of the "member variables" in the class. Of course, attention must be paid to memory alignment
For structure memory alignment, please refer to: < a i=2>Detailed explanation of structure memory alignment In addition: For class objects without member variables, 1 byte is given, and the placeholder does not store actual data, indicating that the object exists


8. this pointer

Insert image description here


With this pointer, the code will be optimized as follows:
Insert image description here


Where does this pointer exist?
exists on the stack because it is a formal parameter!
vs. The this pointer passed below is passed through the exc register, so that this access variable improves efficiency


Interview question: What is the result of compiling and running the following program? A. Compilation error; B. Operation crash; C. Normal operation!
Insert image description here


Insert image description here


Summarize

The above is what I will talk about today. This article introduces the content on classes and objects (with illustrations).
If my blog is helpful to you, remember to support it three times in a row. Thank you everyone for your support!
Insert image description here

Guess you like

Origin blog.csdn.net/2201_75587702/article/details/130651639