Detailed analysis of c++ class (on)

We have learned structure in c language, and we introduced a new concept called class in c++. A class can be understood as an upgraded version of a structure in C language. Why do you say that? In the structure, we can only declare the member variables of the structure, and the functions for operating the member variables of the structure need to be created outside the structure. The class in C++ makes up for this defect very well. Not only can there be member variables in a class, but also member methods (member functions). Let me tell you about the next ones.

Class definition:

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

class is the keyword, and className is the name of the class.

There are two ways to implement member functions in a class. The first is that both the declaration and definition of the function are in the class:

Member functions created by this method are inline functions by default! !

class Stack
{
public:
	void Init()
	{
		//
	}
	void push()
	{
		//
	}

private:
	int* _a;
	int _top;
	int _capacity;
};

 The second is that 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 ::

 

 Speaking of this, some people are curious, why are there public, private, etc. in the code?

Here we involve a new concept: access qualifiers

There are three access qualifiers here: public, protected, private

Public means public, member variables or member functions outside the class can be used.

Protected and private mean protection and private, as if a wall protects the things in the class, and can only be used in the class! ! !

So why is it designed like this?

This shows a major feature of classes: encapsulation.

Encapsulation: organically combine the data and the method of manipulating the data, hide the properties and implementation details of the object, and only expose the interface to the outside world
interact with objects.

So now I ask you a question:

 Since we created a class, it has its own class field, so why can't I access it through the class field?

Reason: When we declare a class with class, it is equivalent to a drawing, and the variables in it do not occupy memory. And when we want to access the data in the domain, it must be real (need to occupy memory). So the solution is as follows, first create a class object (build the drawing into a house), and then access the members through the object (work in the built house).

int main()
{
	Stack s1;
	s1._top = 10;
	return 0;
}

Calculation of class size:

Please calculate the size of the class:

class A {
public:
void PrintA()
{
   cout<<_a<<endl; }
private:
char _a;
};

Let's reveal the answer:

 The answer is 1. There is no doubt that the functions in the class do not occupy space, and the memory size occupied by member variables is calculated by memory alignment like the structures we have learned before. The functions in the class are placed in the public code segment, and when the function is called, it will be found in the public code area. This design saves space! !

When we encounter an empty class, its size is not 0, but 1, remember! ! !

this pointer:

Now we know that functions are stored in the common code area. When we use a class to create two different objects, their member variables are different, but the function they call is the same, so how does this public function distinguish data in different classes? ? Here we want to mention a new concept called this pointer.

 The this pointer is a hidden parameter. When we call a function with an object, we actually pass the address of the object to the this pointer, and the thin pointer finds the data in the object through the address.

d1.Print();  //Print(&d1)
d2.Print();  //Print(&d2)

Since the this pointer is a formal parameter, it is stored in the stack area . Remember that the this pointer can be empty ! ! !

This is the end of the first article about class, I hope everyone will support it, and Kangkang will hurry up to find time to update the next article of class! !

Guess you like

Origin blog.csdn.net/m0_69005269/article/details/127137086