Class
A class is an abstraction and encapsulation mechanism that describes a set of objects that have the same properties and behavior, and is the basic unit of code reuse.
Access rights for class members
One of the object-oriented key characteristics is to hide the data, the mechanism is to set the class members access control permissions. There are 3 types of access rights for class Members:
- Public type: is declared by the keyword public and can only be accessed from outside the class.
- Private type: Declared by the keyword private, a private member can only be accessed by a member function of the class itself.
- Protection type: Declared by protect, similar to a private member, except that a member of a protected type can be accessed by a derived class member function of the class only during the inheritance process.
Note the member functions and class objects of the distinguished class here.
In general, public members are the external manifestation of a class, while private members are internal implementations of the class and do not want to be understood by the outside world.
Implementation of member functions
The data members of the class describe the characteristics of the object, and the member functions of the class determine the operation behavior of the object. The member function is the implementation part of the algorithm, and it is the only way to manipulate the encapsulated data. The implementation method can be divided into class implementation and out-of-class implementation. The out-of-class implementation form is as follows:
Return value type class Name:: member function name (formal parameter table)
{
function body;
}
Class object
An object is an instance of a class, and the system does not allocate storage space for the abstract class, but allocates the appropriate memory space for the object. However, this memory space can only be used to hold the object's data members, whose member functions are not stored in each object's copy.
constructor function
Object creation is more complex than the creation of ordinary variables, which requires that the initial values of the data members be written together while allocating memory space. The function of a constructor is to construct an object with a specific value when the object is created.
The syntax format for declaring a constructor is as follows:
Public
Class name (< parameter table >);
A constructor is a special member function of a class whose function name is the same as the class name, can have any type of argument, but cannot have a return type. When a new object is created, the constructor is automatically called by the compiler. The constructor can be written by itself, and if not provided, the compiler automatically generates a default constructor without parameters (no specific work).
Overloaded constructors:
The so-called overloaded constructors refer to the same constructor name, which has a different parameter table. Note: Avoid ambiguity when constructors have default parameters.
Copy constructor:
A copy constructor is a special constructor that is used to copy objects. It allows you to initialize a similar object that you are creating by using an already created object (specified by the parameters of the copy constructor).
The syntax format for declaring copy constructors is as follows:
Class name
{
Public
Class name (class Name & object name);
};
A copy constructor can have only one parameter and is a reference to a similar object. Each class must have a copy constructor, and if the function simply assigns the value of the created object data member to the data member that is creating the object, then it is not necessary to display the definition of it, and the compiler automatically generates a default copy of the PUP function with the form described above.
Destructors
Destructors are almost the opposite of constructors, and when an object disappears, or when an object created with new is deleted with Delete, the system automatically calls the class's destructor to do some cleanup work. Once it has been called, the object disappears and its corresponding memory space is freed.
The syntax format for declaring a destructor is as follows:
Class Demo
{
Public
Demo (< parameter list >);
~demo (void);
}
The function name of the destructor is the same as the class name, just preceded by a "~". It has no parameters and return values, but can be virtual functions. Because a class can only define one destructor, destructors cannot be overloaded. The destructor call order is the last object created that dies first.
"C + +" classes and objects (construction and destruction)