Learn the definition and implementation of C++ 丨Classes! C/C++ must learn knowledge points!

 

1. Introduction of "Class"

In C++, " class " is used to describe " object ". The so-called "object" refers to everything in the real world. Then the class can be regarded as an abstraction of similar things, to find the commonalities between these different things, such as bicycles and motorcycles. First of all, they are all "objects" and have certain similarities, and some differences, similarities. If they all have quality and two wheels, they are all vehicles. "Both have quality" and "two wheels" belong to the attributes of this object, and "both can be used as vehicles" belong to the behaviors of this object, also called methods .

A class is a user-defined data type, and the data of this type has certain behavioral capabilities, which is the method described in the class. Generally speaking, the definition of a class contains two parts, one is the properties of the class, and the other is the methods it has . In the category of "human", each person has his own name, age, date of birth, weight, etc., which are the attributes of human beings. In addition, the ability of a person to eat, sleep, walk, speak, etc. belong to human behaviors.

The "human" category described in the above example only has some of the most basic attributes and behaviors of human objects, and can be called the "base category" of humans . Let’s talk about some people with some professions, such as students, a student also has attributes that are not in the "base class", such as school, class, student ID; it can also have behaviors that the base class does not have, such as going to class every day , Need exams etc.

The student class can be regarded as an extension of the base class, because it has all the attributes and behaviors of the base class, and on this basis, it adds some attributes and behaviors that the base class does not have. A class like "student" is called " "Human" is a " derived class " or " subclass " of this base class . On the basis of students, Shanghai can further expand other more advanced classes, such as "graduate" classes.

At this point, we will not go deeper to introduce other related knowledge of the class.

 

Two, the definition of C++ class

C++ uses the keyword  class  to define a class, and its basic form is as follows:

 

 

Description:

①. The class name needs to follow the general naming rules;

②.  Public  and  private  are the keywords of attribute/method restriction. Private means that this part of the content is private, cannot be accessed or called by the outside, and can only be accessed inside the class; while public means public attributes and methods, and the outside world can Direct access or call.

Generally speaking, the attribute members of the class should be set to private, and public is only reserved for those function interfaces that are used by the outside world to call, but this is not a mandatory requirement and can be adjusted as needed;

③. The semicolon at the end cannot be omitted.

 

Class definition example:

Define a Point class with the following properties and methods:

■ Attributes: x coordinate, y coordinate

■ Method: 1. Set the coordinate values ​​of x and y; 2. Output coordinate information.

The implementation code is as follows:

 

 

Code description:

In the above code, a class named Point is defined, which has two private attributes, int type xPos and yPos, which are used to represent x point and y point respectively. In terms of methods,  setPoint is  used to set properties, that is, the values ​​of xPos and yPos;  printPoint is  used to output point information.

The following points should be noted when defining the class:

①.  The data members of the class cannot be modified with auto, extern, and register, nor can they be initialized at the time of definition, such as  int xPos = 0; //wrong ;

②.  The order and number of occurrences of private and public keywords in class definition can be arbitrary;

③.  The semicolon at the end cannot be omitted, remember!

 

Three, the realization of C++ class

In the above definition example, we just defined some attributes and method declarations of this class, but did not implement it. The realization of the class is the process of completing its methods. There are two ways to implement a class. One is to complete the definition of member functions when the class is defined, and the other is to complete the definition outside the class.

1>. Define member functions during class definition

The implementation of member functions can be completed at the same time as the class definition, such as the code:

 

 

Run output:

 

 

Compared with the definition of a class, the realization of member functions in a class is no longer to declare in the class, but to define the function directly. When defining a member function in a class, the compiler will try to define it as an inline function by default.  .

 

2>. Define member functions outside the class

Define member functions outside the class by declaring inside the class, and then outside the class through the scope operator  ::  to achieve, the form is as follows:

Return type class name:: member function name (parameter list)

 

 

Change the code in the example to code that defines member functions outside the class:

 

 

According to setPoint member function, in the form of a statement within the class is  void setPoint (int x, int y );  then it outside class when the head-defined functions should be  void Point :: setPoint (int x, int y)  This In this form, the return type, member function name, and parameter list must be consistent with the form declared in the class.

 

Fourth, the use of C++ classes

After a class is defined and implemented, the class can be used to create objects. The creation process is as simple as declaring a variable with basic data types such as int and char. For example, we have a Point class, and to create a Point object, you only need :

Point object name;

Creating an object of a class is called the instantiation of the class. We can also initialize the properties of the object when creating it, so that the object already has certain properties after the creation is completed. This creation method will be next Learn in this blog post.

After the class is instantiated, the system will allocate a certain amount of storage space according to the actual needs of the object. In this way, you can use the object to access or call the properties or methods that the object can provide.

Take the above code as an example. In order to reduce the length, we put the implementation of the Point class in the Point.h header file. The implementation code of the Point class is no longer posted here.

 

 

When the code is compiled, an error will appear, prompting  error:'int Point::xPos' is private , which is   caused by cout<< M.xPos <<endl; this line, he is trying to access the private data xPos in a private object  , If you remove this line, it will run normally.

Through the form of  object name. Public function name (parameter list);  you can call the methods of this type of object, and through   the form of object name. Public data member; you can access the data members in the object.

 

Five, the scope, visibility and life cycle of objects

The scope, visible domain, and life cycle of the class object remain the same as those of ordinary variables. When the object life cycle ends, the object is automatically cancelled, and the occupied memory is recycled. It should be noted that if the object member function uses  new  Or   the dynamic memory program requested by malloc will not release it, and we need to clean it up manually, otherwise it will cause memory leaks.

Guess you like

Origin blog.csdn.net/u010608296/article/details/113108557