C++ articles - class, encapsulation, class access rights, class instantiation


1. Process-oriented and object-oriented

c is a process oriented programming language c++ is an object oriented programming language


Process-oriented: focus on the process, don’t go for solving the problem, call the function to solve the problem step by step . For washing clothes: washing clothes needs to put water, pour washing powder, drain, dry, and dry clothes. In terms of clothing as a whole, there are people involved, such as washing powder, washing machine, and hangers (focus on which ones are involved). A food delivery system is oriented to the process of ordering, putting on shelves,
dispatching, and delivering food (focusing on the process steps) Object-oriented:
Focus on the whole food delivery system, which objects are it (merchants, users, riders), the relationship between them, and the interaction between objects and objects Map real-world classes and objects to virtual computer systems

Two, class

C++ is compatible with the previous grammar of C language. The previous usage of struct can also be used in C++. But in C++, the structure (struct) in the original C language is upgraded to a class. In C++, class is used instead of struct as a
class
: class is the keyword of the class, class className
class represents the class, and className is the class name

#include<iostream>
using namespace std;

struct Stack
{
    
    
	int* a;
	int top;
	int capacity;
};

int main()
{
    
    
	//c语法
	struct Stack st1;

	//c++将struct升级为类,此时Stack为类名,可以直接用其来定义变量(对象)
	Stack st;
	return 0;
}

class domain

Classes form a new scope: the class scope. Different classes have their own different class domains, just like different functions have different local domains, so defining members with the same name in different classes does not constitute redefinition of members in the class. When the previous member accesses the member
behind it, there is no need to declare in advance , because the class domain is a whole, members who access it in the class domain can access it at any time
. The class domain is a whole, regardless of the order of access.

The members of the class have added member functions (methods) compared to the structure, and functions can be directly defined in the class

Take the stack as an example
struct

struct Stack
{
    
    
	//在一般情况下还需要在前面加栈的前缀,但是现在就在这个类中直接将栈的实现写出
	void Init(int defaultcapacity = 4)//类成员函数(方法)
	{
    
    
		a = (int*)malloc(sizeof(int) * defaultcapacity);
		if (nullptr == a)
		{
    
    
			perror("malloc fail\n");
			return;
		}

		top = 0;
		capacity = defaultcapacity;
	}

	void Push(int x)
	{
    
    
		//...扩容
		a[top++] = x;
	}

	void Destroy()
	{
    
    
		free(a);
		a = nullptr;
		top = capacity = 0;
	}

	int* a;
	int top;
	int capacity;
};

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

insert image description here

class

#include<iostream>
using namespace std;

class Stack
{
    
    
public:
	//在一般情况下还需要在前面加栈的前缀,但是现在就在这个类中直接将栈的实现写出
	void Init(int defaultcapacity = 4)
	{
    
    
		a = (int*)malloc(sizeof(int) * defaultcapacity);
		if (nullptr == a)
		{
    
    
			perror("malloc fail\n");
			return;
		}

		top = 0;
		capacity = defaultcapacity;
	}

	void Push(int x)
	{
    
    
		//...扩容
		a[top++] = x;
	}

	void Destroy()
	{
    
    
		free(a);
		a = nullptr;
		top = capacity = 0;
	}

	int* a;
	int top;
	int capacity;
};

int main()
{
    
    
	Stack st;
	st.Init(4);//不可访问,当加了权限访问符(public)就可以访问了
	return 0;
}

insert image description here

It can be found that the class defined by the struct keyword can pass, but the class defined by class cannot pass, and the class defined by class reports an inaccessible error

Access rights
That is because the class has access rights, the three major access rights

public: public permission, external users can access the members of the class
protected: protect the members of the class to prevent external members of the class from accessing
private: only accessible in the class domain, not accessible outside the class domain

With the struct keyword as a class, its default access permission is public (public) permission
With the class keyword as a class, its default access permission is private (private) permission

So in general, you need to use public permissions to access which members of the class you want to use outside the class

What is the scope of access rights? The scope of the access right is from the position where the access character appears to the position where the next access right qualifier appears, and
if there is no next access right qualifier after that, then until the end of the class domain
insert image description here

The class members between public and private are public and can be accessed outside the class, while the public access authority ends at private

encapsulation

Classes encapsulate data and methods for implementing these data, and use classes to combine object properties (data) and methods (member functions) to make objects more complete.
Encapsulate data and methods, so when external users access it, it can Specify which members are open for others to visit, and you can also hide members that you don't want to be accessed by external users, so how does a class make its members accessible to others? Add access rights

Class member function declaration and definition

So class, can its member functions separate declaration and definition? A function that can be declared in a class is defined outside the class domain and needs to have a scope qualifier:: to represent that it is a member function of the class,
insert image description here

And when the function is implemented
, if the variable in the function comes from the class, it needs to find it in the class. When it indicates that the defined function is a member function in the class, the variable in the function is first found in the function, and then in the class domain Find the last global search.
Define a function outside the class domain. Specifying the class domain before the function means that this function is not an ordinary function, but a definition of a member function in the class.

Moreover, when the definition and declaration of a member function of a class are not separated, this function is an inline function. Of course, it is only suggested to the compiler that this function is an inline function. If the code size is particularly large, the compiler will not accept this suggestion
.

General rules for class member names

Class member variables are generally prefixed with _. Since class member functions may be defined with the same name as member variables when they are defined, class and member variables generally start with _

#include<iostream>
using namespace std;
class Year
{
    
    
public:
	void is_year(int year)
	{
    
    
		//先用局部域在用类域
		_year = year;
		cout << _year << endl;
	}

	int _year;
};

int main()
{
    
    
	Year y;
	y.is_year(2002);

	cout << y._year << endl;
	return 0;
}

class instantiation

The member variables in the class only have a declaration, but no definition, so there is no open space, which is equivalent to a house. There are only blueprints, and the real house has not been built, and it cannot be lived in. Only the declaration cannot assign values ​​​​to the declared variables, and define its objects
. It is the instantiation of the class
insert image description here
that does not instantiate the class object, and accesses its member variables, which cannot be accessed at all, which is equivalent to a house drawn on the blueprint, but it is not built in the real world, but only by a A blueprint, I can't find the house

Guess you like

Origin blog.csdn.net/m0_67768006/article/details/130356503