Simple understanding of C++ classes

1. Access permissions for class members

  The three basic characteristics of classes are: encapsulation, inheritance, and polymorphism.
To encapsulate a class, we should also consider the access rights of class members. The so-called access rights are the rights of code outside the class to access members of the class. To make it simpler is whether the code outside the class can access the member variables and member methods in the class.
  C++ controls the access rights of member variables and member functions through the three keywords of public, protected, and private.

private: It means private. The following members are private and cannot be accessed directly by the outside world. The function of the private keyword is to better hide the internal implementation of the class. The exposed interfaces (members that can be accessed through the object) are declared as public, and do not want the outside to know, or only use inside the class, or to the outside Members who are not affected are recommended to declare as private. It is generally accessed through an interface.
Insert picture description here

protected: It means that it is protected and cannot be accessed directly by the outside world.
public: It means public, and can be accessed by the code outside the class department.

Insert picture description here

  Inside the class, that is, in the member functions of the class, no matter whether the members are declared as public, protected or private, they can all visit each other, and there is no restriction on access rights. This is like a family, and each family can visit each other's rooms.

But outsiders can't. For example, if an outsider wants to borrow a book from me, he can't directly enter my room to get the book. I or my family can give it to him.

2. Naming of member variables

  Most of the member variables start with m_, which is a conventional way of writing, not a grammatical requirement. Starting with m_ can be seen at a glance that this is a member variable, but also can be distinguished from the parameter name in the member function.

Insert picture description here

3. Constructor

  It is often used to do some initialization work, to initialize member variables, etc. Note that you cannot use memset to initialize the class.

  The constructor has the following characteristics:

1) The constructor must be a public attribute.

2) The constructor has no return value, because there is no variable to receive the return value, even if there is, it is useless, no matter whether it is a declaration or a definition, the return value type cannot appear before the function name, even if it is void, it is not allowed.

3) The constructor can have parameters, allowing overloading. A class can have multiple overloaded constructors. When creating an object, it determines which constructor to call based on the passed parameters.

4) It is automatically executed when the object is created (automatically jumps to the constructor execution). For example, when debugging with gdb, set the breakpoint in the constructor. When the program stops at the breakpoint and press "s" to continue, it will jump to the constructor

Set the breakpoint on line 42
Insert picture description here

When you press "s" to continue, it will automatically jump to line 65 of the constructor
Insert picture description here

4. Destructor

  It is automatically executed when the object is destroyed, and is used for cleaning up (so it can be called explicitly), such as releasing allocated memory, closing open files, etc. This purpose is very important and can prevent programmers from making mistakes.

The destructor has the following characteristics:

1) The constructor must have a public attribute.

2) The constructor has no return value, because there is no variable to receive the return value, even if there is, it is useless, no matter whether it is a declaration or a definition, the return value type cannot appear before the function name, even if it is void, it is not allowed.

3) The destructor does not allow overloading. A class can only have one destructor.

Guess you like

Origin blog.csdn.net/qq_43403759/article/details/113399022