C++ classes and objects (introduction of classes, this pointer)

Process Oriented and Object Oriented

process oriented

C language is process-oriented, focusing on the process, just like in a food delivery system, focusing on the three processes of placing an order, receiving an order, and delivering food, each process is realized by a function.

object oriented

Similar to java, it is a purely process-oriented language, which focuses on the three parties of merchants, users, and riders and their interactions. It is achieved by the design of the class and the relationship between the classes.
And our C++ is both object-oriented and procedure-oriented (compatible with C)

Three characteristics of object-oriented

Encapsulation, inheritance and polymorphism.

class introduction

keywords

There are two kinds of keywords that can be used to define a class: struct and class.
The difference between the two defining classes is that, without using access qualifiers, the content in the class defined by struct is public by default (the content in the class can be accessed in the main function), while the content in the class defined by class is private by default ( The content of the class cannot be accessed in the main function).

access qualifier

There are three access qualifiers: public (public), protected (protected), private (private)

1. Public modified members can be accessed directly outside the class. (Public)
2. Protected and private modified members cannot be directly accessed outside the class. (Private)
3. The scope of access rights is from the position where the access qualifier appears to the next access qualifier, if there is no access qualifier below, then the class ends.
4. The default access permission of class is private (private), and struct is public (public).
5. In the actual definition, it is not recommended to use the default access rights, and try to use the access qualifier.

class definition

In the C language, struct can be used to define a structure, but the structure can only have variables and not functions, but in a class, there can be both variables and functions.
If there is no function in a class, it can be regarded as both a structure and a class in C++, and the usage rules of structures and classes are satisfied at the same time.

struct Student
{
    
    
	char _name[10];
	int _age;
	int _id;
	void Init(const char* name, int age, int id)
	{
    
    
		strcpy(_name, name);
		_age = age;
		_id = id;
	}
	void Print()
	{
    
    
		cout << _name << endl;
		cout << _age << endl;
		cout << _id << endl;
	}
};

This is the class named Student defined using the struct keyword, which is divided into a member variable area and a member function (function) area.
Note that member variables can be accessed in member functions. When the parameter names are the same, in order to avoid indistinguishability, a _ is usually added when defining member variables.

If the member variable name _year is changed to year, in the statement year=year in the Init function, both years will represent the year of the formal parameter (the principle of proximity), which is equivalent to assigning a value to yourself, but not to the object. The purpose of assigning year in the object, if you want to assign a value to the year in the object, you need to write: Data::year=year
insert image description here
Because object-oriented programming has a sense of encapsulation, when defining a class, try to use class to define it, and set it first. To be private (the main function cannot access its contents), use the access qualifier to make a certain part public.
Usually member functions are made public and member variables are made private.

class Student
{
    
    
private:
	char _name[10];
	int _age;
	int _id;
public:
	void Init(const char* name, int age, int id)
	{
    
    
		strcpy(_name, name);
		_age = age;
		_id = id;
	}
	void Print()
	{
    
    
		cout << _name << endl;
		cout << _age << endl;
		cout << _id << endl;
	}
};

The scope main function of the private role cannot be accessed, and the scope main function of the public role can be accessed.
Note that functions in a class can access variables in the class. Regardless of access qualifiers.

class usage

In the main function, the usage of the class is the same as that of the structure, and the "." operator is used for calling parameters or calling functions.

int main()
{
    
    
	Student s1;
	s1.Init("zhangsan", 1, 2);
	s1.Print();
}

insert image description here
Defined variables such as s1 are habitually called objects.

class scope

A class defines a new scope, and all members of the class are in the scope of the class. To define a member outside the class body, you need to use the ::scope resolver to indicate which class scope the member belongs to.
For example, in the header file declare this:

class Student
{
    
    
private:
	char _name[10];
	int _age;
	int _id;
public:
	void Init(const char* name, int age, int id);
	void Print();
};

In the .cpp file refer to the writing function like this:

void Student::Init(const char* name, int age, int id)
{
    
    
	strcpy(_name, name);
	_age = age;
	_id = id;
}
void Student::Print()
{
    
    
	cout << _name << endl;
	cout << _age << endl;
	cout << _id << endl;
}

insert image description here
Note how the citations are made here.

How classes are stored

The class contains both member variables and member functions, but only member functions are stored in memory without storing member variables. This is to prevent the waste of multiple objects calling member functions.
Member functions are stored in the common code area.
insert image description here
When calculating the size of the object, only the size of the space occupied by the member object needs to be calculated, and the calculation method is the same as that of the C language to calculate the size of the structure. This article on learning custom types in C language is enough (the 40-word summed up with painstaking efforts) [Salute: We still believe in light]
Note that when there are no member variables in the class, the size of the class is 1 byte, and this 1 byte does not store valid data , only for placeholders, indicating that the object exists.

this pointer

The introduction of this pointer

Take a look at the following piece of code:

class Data
{
    
    
private:
	int _year;
	int _month;
	int _day;
public:
	void Init(int year, int month, int day)
	{
    
    
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
    
    
		cout << _year << " " << _month << " " << _day << endl;
	}
};
int main()
{
    
    
	Data d1;
	Data d2;
    d1.Init(2022, 2, 25);
	d2.Init(2022, 2, 26);
	d1.Print();
	d2.Print();
	return 0;
}

We all know that when an object is defined, only member variables and not member functions are stored in the object, so when the Print function is called, how does the compiler know whether to print the value in d1 or the value in d2?
This requires the introduction of the concept of this pointer. In fact, in the Print() function, it is not that there are no parameters, but the parameters are hidden. The real situation handled by the compiler is: d1.Print(&d1), and the pointer that receives the address of d1 We call it this pointer, and it is of type **const Data* called this. That is, the function defined in the class is really: Print(const Data this).
Inside the function, when printing, it actually prints this->_year, this->_month.
Similarly, in the Init function, the this pointer will also be passed in. The actual input is actually: d1.Init(&d1,2022,2,25)
Note: Although the compiler defaults to this, you cannot pass the this pointer like this. Write it in d1.Init(&d1,2022,2,25), d1.Print(&d1), Print(const Data
this), if you must write it, you can write this->_year, this->_month inside the function .
insert image description here

Properties of this pointer

1. The type of this pointer: const class name*.
2. Can only be used inside member functions.
3. The this pointer is actually a formal parameter of a member function. When the object calls the member function, the object address is passed as an actual parameter to the this parameter, so the this pointer is not stored in the object. The this pointer is usually stored on the stack, and some compilers also store it in a register.

this pointer example

What is the result printed by the code:

Example 1

class A
{
    
    
public:
	void Show()
	{
    
    
		cout << "Show()" << endl;
	}
private:
	int _a;
};
int main()
{
    
    
	A* p = nullptr;
	p->Show();
}

insert image description here
This code can be successfully printed. Note that although p is a null pointer, it can also access the methods in the class, but cannot access the variable . Here the this pointer receives a null pointer, but it is received without dereferencing it. , so it can be printed normally.

Example 2

class A
{
    
    
public:
	void PrintA()
	{
    
    
		cout << _a << endl;
	}
private:
	int _a;
};
int main()
{
    
    
	A* p = nullptr;
	p->PrintA();
}

Similarly, the this pointer receives the address of p, but in the function Print, the this pointer is dereferenced (this->_a), so the program will crash.

Guess you like

Origin blog.csdn.net/qq_51492202/article/details/122527223