Part 1 of classes and objects (easy to understand)


Preface

C language is process-oriented and uses functions to solve problems step by step. C++ is object-oriented and solves problems through the interaction between various objects.


1. Basic concepts of classes:

1. Introduction of classes:

In C language, we all know that variables can be defined in the struct structure. But in C++, not only variables but also functions can be defined in the structure.

struct Stack
{
    
    
	void Init(int capacity = 5)
	{
    
    
		_array = (int*)malloc(sizeof(int) * capacity);
		if (nullptr == _array)
		{
    
    
			perror("malloc申请空间失败");
			exit(-1);
		}
		_capacity = capacity;
		_top = 0;
	}
	int* _array;
	int _capacity;
	int _top;
};

2. Class definition:

C++ prefers to use class to define classes:

class ClassName//类名
{
    
    
// 类体
}; 
  1. class is the keyword that defines the class
  2. ClassName is the class name
  3. The class body contains member variables and member functions

Two ways to define a class:
Insert image description here
The class defines a new scope, and all members of the class are in 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.

3. Class access qualifiers and encapsulation:

Let’s first talk about what encapsulation is: organically combining data and methods of operating data, hiding the properties and implementation details of the object, and only exposing the interface to interact with the object.

Using access qualifiers on classes can make better use of the class, providing its interface to users through access selectivity, and encapsulation also makes the class more secure.

There are three types of access qualifiers for classes:

  1. public
  2. protected
  3. private

illustrate:

  • Members modified by public can be accessed directly.
  • Members modified by private and protected cannot be directly accessed outside the class (the two are similar in this regard).
  • Access permissions scope from the current access qualifier to the end of the next access qualifier.
  • If there are no subsequent access qualifiers, the scope extends to the end of the class.
  • The default access permission of class is private, and the default access permission of struct is public.

4. Instantiation of class:

The process of creating an object from a class is called instantiation of the class.

  1. A class is a description of an object. A class is like a construction drawing of a house, and the object is the actual built house.
  2. A class can instantiate multiple objects, and the objects occupy actual physical space.
  3. Classes have no space, only instantiated objects have space.

2. this pointer:

1. Extraction of this pointer:

Look at the following code: the two objects s1 and s2 call the Init function respectively. How does the function distinguish them?

int main()
{
    
    
	Stack s1;
	Stack s2;
	s1.Init();
	s2.Init();
	return 0;
}

C++ uses this pointer to solve this problem. When calling a function, the compiler will automatically pass the address of the object as a parameter. At the same time, the function will use this pointer internally to accept it. The this pointer type is *const, and this itself cannot be modified. These are compiled It is automatically implemented by the server and does not require the user to pass it manually.
Insert image description here

2.Characteristics of this pointer:

  1. The this pointer type is * const and cannot be modified.
  2. Can only be used in member functions
  3. This pointer is actually a formal parameter of the member function and does not exist in the object.
  4. This pointer does not need to be passed manually by the user, the compiler will automatically implement it.

Summarize

The above is what we talked about today about classes and objects. It mainly explains the basic concepts of classes and objects and the this pointer. I hope it will be helpful to you who have just read this blog!

Guess you like

Origin blog.csdn.net/weixin_61661271/article/details/126454376