on classes and objects

1. Class definition

  class is the keyword to define the class, ClassName is the name of the class, and {} is the subject of the class. The contents of the class body are called members of the class: variables in the class are called attributes or member variables of the class; functions in the class are called methods or member functions of the class.

1. All declarations and definitions are placed in the class body

  Note: If a member function is defined in a class, the compiler may treat it as an inline function

2. The class declaration is placed in the .h file, and the member function definition is placed in the .cpp file

Note: The class name needs to be added before the member function name::

Second, the class access qualifier

illustrate:

  1. Publicly modified members can be directly accessed outside the class
  2. Protected and private modified members cannot be directly accessed outside the class (here protected and private are similar)
  3. Access scopes start at the occurrence of this access qualifier until the next occurrence of the access qualifier
  4. If there is no access qualifier behind, the scope will go to }, which is the end of the class.
  5. The default access permission of class is private, and struct is public (because struct is compatible with C)

3. The scope class of the class and its instantiation

1. Scope

  A class defines a new scope, and all members of the class are in the scope of the class.When defining members outside the class, you need to use the :: scope operator to indicate which class domain the member belongs to

2. Instantiation

The process of creating an object with a class type is called instantiation of the class:

  1. A class is a description of an object. It is a model-like thing, which limits the members of a class, and defines a class without allocating actual memory space to store it;
  2. A class can instantiate multiple objects, and the instantiated objects occupy the actual physical space. The storage class member variable Person class has no space, and only the objects instantiated by the Person class will occupy space.
  3. For example. Instantiating an object from a class is like building a house using architectural design drawings in reality. Classes are like design drawings. Only the instantiated objects can actually store data and occupy physical space.

Fourth, the calculation of the class object size

1. Calculation object (only member variables)

  The size of a class is actually the sum of "member variables" in the class. Of course, attention should be paid to memory alignment. The empty class is special. The compiler gives the empty class a byte to uniquely identify the object of this class. Notice:Only member variables are saved in the class, and member functions are stored in the public code segment

2. Calculation method (structure alignment rules)

Structure memory alignment rules:
1. The first member is at the address whose offset from the structure is 0.
2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number).
Note: Alignment = the smaller value between the compiler's default alignment and the member size.
The default alignment number in VS is 8.
3. The total size of the structure is: an integer multiple of the maximum alignment number (the largest of all variable types and the smallest default alignment parameter).
4. If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment number, and the
overall maximum alignment number (including the alignment number of the nested structure) Integer multiples.

5. The this pointer of the class member function

1. Generation of this pointer

  For the following date class, think about such a question?
  There are two member functions Init and Print in the Date class, and there is no distinction between different objects in the function body. Then when d1 calls the Init function, how does the function know that d1 should be set? object instead of setting the d2 object?

class Date
{
    
    
public:
void Init(int year, int month, int day)
{
    
    
	_year = year;
	_month = month;
	_day = day;
}
private:
	int _year;    
	int _month;    
	int _day;      
};
int main()
{
    
    
	Date d1, d2;
	d1.Init(2022,1,11);
	d2.Init(2022, 1, 12);
	return 0;
} 

  In C++, this problem is solved by introducing this pointer, namely: the C++ compiler adds aHidden pointer parameter (this pointer), let the pointer point to the current object (the object that calls the function when the function is running), all "member variable" operations in the function body are accessed through this pointer. It's just that all operations are transparent to the user, that is, the user does not need to pass it, and the compiler completes it automatically

2. Characteristics of this pointer

Features:
1. The type of this pointer: class type * const, that is, in member functions, the this pointer cannot be assigned a value.
2. It can only be used inside the "member function"
3. The this pointer is essentially the formal parameter of the "member function". When the object calls the member function, the object address is passed as the actual parameter to the this formal parameter. So the this pointer is not stored in the object.
4. The this pointer is the first implicit pointer parameter of the "member function". Generally, it is automatically passed by the compiler through the ecx register and does not need to be passed by the user.

Notice:this cannot be explicitly passed between formal parameters and actual parameters, but it can be used inside the function, and this pointer cannot be modified by assignment

Summarize:

  C++ classes and objects are the foundation of object-oriented programming and one of the core concepts in C++. Mastering the definition and scope of classes and objects is just basic knowledge. In the next article, we will learn more about construction, destruction and other related knowledge. Classes are the abstraction and description of objects, and the two complement each other to form the core of C++ object-oriented. Through continuous learning and practice, I believe that everyone can master the classes and objects in C++ programming, and provide a better foundation and support for their own programming.

Guess you like

Origin blog.csdn.net/Front123456/article/details/130472896