C++ class and object &&this pointer

foreword

From this article onwards, the chapters on C++ classes and objects began, um, so much

insert image description here

one type

1. Introduction of class

Only variables can be defined in the C language structure. In C++, not only variables but also functions can be defined in the structure. For example:
in the initial stage of data structure, the stack implemented in C language can only define variables in the structure; now it is implemented in C++, you will
find that functions can also be defined in struct

#include<iostream>
using namespace std;

typedef int DataType;
struct Stack
{
	void Init(size_t capacity)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	void Push(const DataType& data)
	{
		// 扩容
		_array[_size] = data;
		++_size;
	}
	DataType Top()
	{
		
			return _array[_size - 1];
	}
	void Destroy()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
	DataType* _array;
	size_t _capacity;
	size_t _size;
};
int main()
{
	Stack s;
	s.Init(10);
	s.Push(1);
	s.Push(2);
	s.Push(3);
	cout << s.Top() << endl;
	s.Destroy();
	return 0;
}

2. Class definition

class is the keyword to define the class, ClassName is the name of the class, and {} is the body of the class. Note that the semicolon after the end of the class definition
cannot be omitted.
The content in the class body is called the members of the class: the variables in the class are called the attributes or member variables of the class; the functions in the class are called the methods or member
functions of the class.

class className
{
// 类体:由成员函数和成员变量组成
}; // 一定要注意后面的分号

Note that the declaration and definition of the class should be divided into two files, the declaration is in the header file of .h, and the definition is in the source file of .cpp. The specific implementation is the same as the separation of declaration and definition in C language.

3. Class 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.

insert image description here

4. Class access qualifiers and encapsulation

encapsulation

The three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

Encapsulation: organically combine data and methods of manipulating data, hide object attributes and implementation details, and only expose interfaces to
interact with objects.

For example: a mobile phone is a packaged thing. There are memory cards, CPUs, etc. in the mobile phone, and they are packaged together to form a mobile phone.

access qualifier

The way of C++ to achieve encapsulation: use classes to combine the properties and methods of the object to make the object more perfect, and selectively
provide its interface to external users through access rights

  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)

interview questions

What is the difference between struct and class in C++?

Answer: C++ needs to be compatible with C language, so struct in C++ can be used as a structure. In addition, struct in C++ can also be used to
define classes. It is the same as class definition class, the difference is that the default access right of class defined by struct is public, and the default access right of class defined by class
is private.

Second, this pointer

1.this pointer definition

The C++ compiler adds a hidden pointer parameter to each "non-static member function", so that the pointer points to the current object (the object that calls the function when the function is running), and all "member variable" operations in the function body, All 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

class Date
{
    
    
public:
    Date()
    {
    
    
        _year = 1900;
        _month = 1;
        _day = 1;
    }
    int  Get_day()
	{
    
    
		return _day;
	}
    //当被调用时系统会转换成以下样子
    //int  Get_day(const Date* this)
	//{
    
    
	//	return _day;
	//}
	int Get_month()
	{
    
    
		return _month;
	}
	int Get_year()
	{
    
    
		return _year;
	}
   
private:
    int _year;
    int _month;
    int _day;
};

void Test()
{
    
    
	Date d1;
    int day=d1.Get_day();;//当调用时会变成Get_day(&d1)
}

2. Characteristics of this pointer

1. The type of this pointer: the type is * const, that is, the this pointer cannot be assigned a value in a member function.

2.只能在“成员函数”的内部使用

3. This pointer is essentially a formal parameter of a "member function". When an object calls a member function, the object address is passed as an actual parameter to

4. this parameter. So the this pointer is not stored in the object (stored in the stack).

5. The this pointer is the first implicit pointer parameter of the "member function", which is usually ecx寄存器automatically passed by the compiler

hand over,不需要用户传递

Guess you like

Origin blog.csdn.net/Ruiren_/article/details/130411570