C ++ class related issues, constructor and destructor, copy constructor instance explanations, constructor instance explanations
The basic task of object-oriented is to describe the object and classify and summarize the object. The class type is the same as the int type, and there is no memory allocation. Class attributes and external interfaces are the key and difficult points of class definition. The principle is to privatize internal operations as much as possible and provide easy-to-use interface functions.
1. Questions about classes
When defining data members of a class, initialization cannot be performed like defining variables. If the access qualifier is not specified during definition, the default value is private;
When defining a class method, if the method does not need to modify the class data member, it is best to use the const keyword at the end of the method declaration, indicating that the user cannot modify the class data member in this method. If the class contains pointer members, you cannot assign a value to the pointer again in the const method, but you can modify the data in the address pointed to by the pointer.
Class, except for direct access by static members, other members access through objects. No storage space is allocated when a class is defined. The storage space is allocated only when an object is instantiated. You can also declare a class object as a pointer and use? The new operator allocates memory for it as follows:
If you declare a class object as a constant pointer, you can only call the constant method in the class.
2. constructor and destructor
Each class has constructor and destructor. constructor is called when an object is defined, and the constructor is called when the object is released. The constructor is responsible for initialization before the class object is generated, and the Destructor is responsible for processing the destroyed object.
The constructor does not return values. If you do not provide constructor and destructor, the system uses the default constructor. A class can contain multiple constructors, which are distinguished by overloading. The following is a pointer to call the parameter constructor for initialization:
Data members defined in the class body cannot be initialized, so class initialization can only be implemented by using the initialization list of the constructor. If defined in the class body, this member function is considered to be an inline function even if inline is not used before the function.
The Destructor does not return values or parameters, so it cannot be overloaded.
3. copy constructor
The replication constructor is similar to other functions of the class. The class name is used as the function name, and the first parameter is the constant reference type of the class.
The following three situations require the replication constructor:
The object passes in function parameters as values;
The object is returned from the function by passing the value;
The object needs to be initialized through another object;
The compiler has a default copy constructor. This function only assigns values to the "new object" data members one by one using the values of the data members of the old object. This is called a shortest copy, that is, only the data members in the object are assigned a simple value. This method may cause problems when there are dynamic members in the object, such as pointers in the object. The shallow copy only enables the two pointers to have the same value, instead of the two different addresses we need.
Deep copy: for the dynamic members of an object, not only the assignment value, but also the dynamic allocation of space, so that the Pointer Points to two different memories, but the memory value is the same.
When writing a function, try to pass parameters by reference. This avoids calling the copy constructor and greatly improves the program efficiency. You can also put the copy function in private to prevent the default copy.
4. Static class members
Common class members can only access the object Through instantiation. Static class members can also access the object directly using the class name, and use the domain access character. When defining static data members, initialize static data members outside the class body. Static data members are shared by all class objects.
Static data members can be of the current type, while other data members can only be pointers or reference types of the current class, such:
The following table lists static data members:
Static data members can be used as default parameters of member functions, but normal members cannot;
The static member function of a class can only be a static member of a category and cannot access common data members;
The end of the static member function cannot be modified with the const keyword;
When static data members and static member functions are initialized or defined outside the class body, the static keyword is removed;
5. Operator Overloading
Operator Overloading requires the operator keyword, which is actually a type of function overloading, because the operator is a function. For heavy-duty operators, two functions cannot exchange order. The order in which the heavy-duty operator is called can only be used in this order.
For the ++ and -- operators, because the front and back operations are involved, by default, the overload operators do not have parameters, indicating that they are pre-operations. If an integer int is used as a parameter, they represent post-operations.
Not all operators can be reloaded. Most operators can be reloaded, ::,?, :,. Cannot be reloaded.
6. sizeof questions about classes
The empty class will also be instantiated, And the compiler will implicitly Add a byte to the class, so the sizeof () Result of the empty class is 1; sizeof () is used to calculate the length of the string and contains "\ 0 ", the length of strlen () does not include "\ 0 ".
Constructor and destructor are not included in the statistical range of sizeof.
To maintain the position of a virtual function in the virtual function table, it must occupy the size of a pointer.
Static members are not included in the sizeof () Statistical range.
In general, the class size is related to the non-static member size and virtual function, and is irrelevant to other common member functions. The class size also complies with the byte alignment rules for memory allocation:
7. youyuan and youyuan Methods
When you want another class to access the private members of the current class, you can use another class as your friend class in the current class. Youyuan is a friend. Private Members can only be accessed by friends.
If you only want the private member of a member function member class, you can declare this function as a friend function of the class, that is, add the friend keyword before the return value of the function. Youyuan functions can be both class member functions and global variable functions.
8. Differences between references and pointers
A reference is the alias of a variable. The reference must be initialized at the same time, and the pointer can be initialized at any time;
A null reference is not allowed. The reference must be associated with a valid storage unit. A pointer can be NULL;
Once a reference is initialized, the reference relationship cannot be changed, and the pointer can change the object that it refers to at any time.
9. class inheritance
Inheritance is one of the main characteristics of object-oriented objects. It enables a class to be derived from an existing class without the need to redefine a new class. The ":" operator is used for class inheritance.
The following table lists the relationships between class inheritance and access:
When using public inheritance, the types of public and protected in the original class remain unchanged in the derived class;
When protected is used for inheritance, the public and protected types in the original class are changed to the protected type in the derived class;
When private is used for inheritance, both public and protected in the original class become private in the derived class;
When a user derives a subclass from the parent class, there may be a situation where a method with the same name as the parent class is defined in the subclass. In this case, the subclass hides the parent class method, in this way, all methods with the same name in the parent class, including overload methods, are hidden. To access methods in the parent class, use the domain access method.
After deriving a subclass, you can define a parent class pointer and create an object for it through the subclass constructor. Because the compiler is statically bound to methods with the same name, that is, the class method to be called is determined based on the type defined by the object.
When defining a method, use the virtual keyword before the method. This is a virtual method. You can use the virtual method to implement dynamic binding, that is, determine the method of the class to be called based on the type of the object running. The virtual method defined in the parent class. The method with the same name in the subclass is also a virtual method even if there is no virtual keyword before.
Pure virtual method:
In C ++, apart from virtual methods, pure virtual methods can also be defined, that is, abstract methods. A class containing pure virtual methods is called an abstract class. Abstract classes cannot be instantiated. They cannot be returned values or function parameters, but can be set as pointers. They are often used in interface implementation.
An abstract class is often used as the parent class of other classes. The subclass derived from the abstract class must implement all pure virtual methods in the parent class. The significance of defining a pure virtual function is to tell the subclass that this interface must be inherited and rewrite the function body to enhance its functions.
10. Create and release subclass objects
When a child class is derived from the parent class and a subclass object is defined, it calls the parent class constructor and the subclass constructor in sequence. When the object is released, the Child class constructor is called first, then, call the parent class destructor;
When defining a parent class object and calling a subclass to create an object, it calls the parent class constructor and the subclass constructor in sequence. There are two situations when releasing:
1. If the Destructor is not a virtual function, only the parent class destructor is called;
2. If the Destructor is a virtual function (parent class), the sub-class destructor are called before the parent class destructor.
It can be seen that if the parent class is not a virtual destructor, the Child class is allocated space. If no class destructor is called, memory leakage will occur. Therefore, when writing a function, common virtual functions of destructor.
11. Multi-Inheritance
Multi-inheritance is a subclass that can inherit multiple parent classes. Each parent class is separated by a comma and must contain keywords. If there are methods of the same name in different parent classes, specify the name of the parent class when accessing the subclass instance. Multiple inheritance chains cannot have loops. You can use UML to draw an inheritance diagram.
During Multi-inheritance, there are usually two parent class backups in the third generation subclass. C ++ provides a virtual Inheritance Mechanism to make only one backup. When the two child classes inherit the parent class, add the virtual keyword. Virtual inheritance does not create constructor from the first generation parent class, but directly starts from two subclasses.
12. Class templates
The class template can provide a dynamic parameterization Mechanism for the data members, member function parameters, and return values of the class, so that users can easily design more flexible classes.
You can also set static data members in a class template. Different types of template instances have their own static data members, and instances of the same type share static data members.