"C++" classes and objects 1

  • Class introduction

We all know that C language is a process-oriented language, and C++ is an object-oriented language, so what is process-oriented and object-oriented? Process-oriented is to focus on the process, analyze the steps to solve the problem, and gradually solve the problem by calling functions, while object-oriented focuses on the object, splitting a thing into different objects, and completing it by the interaction between the objects.
In C language, we use the keyword struct to define structures. Structures can only define variables. What should we do if we define functions? In C++, we can define not only variables but also functions in structures, and we prefer to use them in C++ The keyword class is used instead.

  • Class definition

How to define a class? The format is as follows

class classname//类的名字
{
    
    
	//类体:成员变量和成员函数组成
};

The elements in a class are called members of the class, the data in the class are called attributes or member variables of the class, and the functions in the class are called methods or member functions of the class. I prefer to call them member variables and member functions respectively.
There are two ways to define a class:
1. The declaration and definition are all placed in the class body. It should be noted that if the member function is defined in the class, the compiler may treat it as an inline function.

class person
{
    
    
	public:
	void show()//声明和定义全部放在类体中
	{
    
    
		cout<<_name<<"-"<<_sex<<"-"<<_age<<endl;
	}
	public:
	char* _name;
	char* _sex;
	int _age;
};

2. The declaration is placed in the .h header file, and the class definition is placed in the .cpp file (recommended method)

//person.h文件
class person
{
    
    
	public:
	void show()
	public:
	char* _name;
	char* _sex;
	int _age;
};

//person.cpp
#include"person.h"
void person::show()
{
    
    
	cout<<_name<<"-"<<_sex<<"-"<<_age<<endl;
}
  • Class access qualifiers and packaging

First ask the question: What is the difference between struct and class in C++?
C++ needs to be compatible with the C language, so in C++ struct can be used as a structure and can also define classes. It is the same as the class definition class. The difference is that the default access method for members in struct is public, and the default access for members in class The method is private.

Public and private are introduced here, which are the access qualifiers of the class. There are three types of access qualifiers: public (public), private (private) and protected (protected).
Access qualifier description:

  • Members modified by public can be directly accessed outside the class
  • The protected and private modified members cannot be directly accessed outside the class (the two are similar)
  • The scope of access rights starts from the position where the access qualifier appears until the next access qualifier appears
  • Access qualifiers are only useful at compile time. After the data is mapped to memory, there is no difference in access qualifiers.

So we can think of the way to achieve encapsulation in C++ is to combine the attributes and methods of the object with classes to make the object more complete, and selectively provide its interface to external users through access permissions.
Encapsulation is essentially a kind of management, which is similar to the management of a museum. If a pile of cultural relics is placed in one place for people to see, it may be gone after a period of time without management, so the museum will be built to collect these cultural relics. Encapsulation, the purpose is not to encapsulate all of them so that they cannot be seen, so a ticketing channel is opened for everyone to visit under supervision, and not all cultural relics can be seen. Some cultural relics cannot be seen, that is, they are completely encapsulated. Up. This is the same as a class. Use private/protected to encapsulate members and open some public members for access, so the essence of encapsulation is a kind of management.

Guess you like

Origin blog.csdn.net/NanlinW/article/details/103169380