[C++] Object-oriented model ① (Object-oriented design | Object-oriented underlying implementation mechanism | Program memory structure)





1. Object-oriented model




1. Object-oriented design


Support for object-oriented programming in the C++ language includes:

  • Encapsulation: encapsulate static/non-static member variables/member functions
  • Inheritance: single inheritance, multiple inheritance, virtual inheritance;
  • Polymorphism: function overloading;
  • Creation and recycling of objects: constructors, destructors;

2. Object-oriented underlying implementation mechanism


In the C language, data (variables) and behavior (functions) are declared separately. There is no correlation between data and behavior. The C language does not support the encapsulation of data and behavior;

In the C++ language, you can define "abstract data types", and you can define data (variables) and behaviors (functions) in classes, encapsulate them together, and realize the binding of data and behaviors. The two are related;


Member variables in the C++ object model can be

  • Public: can be directly accessed by objects;
  • Private: can only be accessed by the member functions of the object itself;
  • Protected: can only be accessed by the object's own member functions and derived classes;

There are two types of member variables in C++:

  • Static member variables: member variables modified with static;
  • Non-static member variables: They are ordinary member variables;

There are 3 types of member functions in C++:

  • Static member function: Member function modified with static;
  • Non-static member function: ordinary member function;
  • Virtual function: a member function modified with virtual;

3. Program memory structure


Actual computer program structure: From the perspective of computer program execution, a program consists of data segments and code segments;

Object-oriented encapsulation structure: In object-oriented design in C++, variables and functions are encapsulated together;

The C++ compiler needs to handle the work of converting "object-oriented encapsulation structure" into "actual computer program structure";


The object model of C++ describes how C++ organizes and manages objects in the program, including

  • Object memory layout
  • Access to member variables
  • member function call
  • Virtual function mechanism, etc.

Multifaceted issues;


Each C++ class instance object has its own memory space, including member variables and member functions;

Member variables are the data in the object, and member functions are the behaviors in the object;


4. Code example - object memory structure test


Execute the following code example,

The size of all classes is 12 bytes, and there are only 3 int type member variables that are the size of the class.

You can see that the memory size occupied by the class is only related to ordinary objects. Static member variables and member methods are not defined in the object;


Code example:

#include "iostream"
using namespace std;

class A
{
    
    
public:
    int a;
    int b;
    int c;
};

class B
{
    
    
public:
    int a;
    int b;
    int c;
    static int s;
};

class C
{
    
    
public:
    int a;
    int b;
    int c;
    static int s;

public:
    int fun() {
    
     return 4; };
    void fun2() {
    
    };
};

struct D
{
    
    
    int a;
    int b;
    int c;
};

struct E
{
    
    
    int a;
    int b;
    int c;
    static int s;
};

int main() {
    
    

    cout << " sizeof(A) = " << sizeof(A) << endl;
    cout << " sizeof(B) = " << sizeof(B) << endl;
    cout << " sizeof(C) = " << sizeof(C) << endl;
    cout << " sizeof(D) = " << sizeof(D) << endl;
    cout << " sizeof(E) = " << sizeof(E) << endl;
	

    // 控制台暂停 , 按任意键继续向后执行
    system("pause");

    return 0;
}

Results of the :

 sizeof(A) = 12
 sizeof(B) = 12
 sizeof(C) = 12
 sizeof(D) = 12
 sizeof(E) = 12
请按任意键继续. . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/133210850