View C + + as a language federation--"effective C + +"
OOP is an important part of C + + and is more and more complex. Summarize the structure of knowledge that exists in my mind
1. Access specifier
I. Intra-class
Public: The most open permission, which is accessible within the class, is also accessible for instances of the class.
Private: The most restrictive permission, only the class internal and friend can access
protected: Can be treated as a product of public and private neutralization , accessible within a class, accessible within a derived class ( but it can access only the part of the base class corresponding to the protected of the derived class object, for a complete base class object S, It is not possible to access the protected in S ), the instantiated object of the class cannot be accessed
Second, class derived from the list
The access specifier in the class-derived list has no effect on the class, it only affects the instantiated object of the class.
2. Structure, copy construction and destructor
What happened to the process of calling the subclass constructor and copy constructor?
When a subclass constructor is called, it calls the constructor of the direct base class first, and if the direct base class has a base class, the constructor of the indirect base class continues to be called.
What happens in the process of calling a subclass destructor?
It will first destructor the subclass object itself, then call the destructor of the base class, and so on, along the inverse direction of the integrated system
PS: For classes whose class members are user-defined types, its construction and destruction process is similar to the above. Construction process: First constructs the class member, after the member constructs completes, constructs itself, the destruction process: first analyzes the structure itself, then analyzes the constituent member.
3. Inheritance and type conversion
A derived class can convert to a base class, but in this process, the members that are unique to the derived class are slice down (cut off) and vice versa.
If you do not want a method in a class or class to be inherited, you can use the keyword final
4. polymorphic
An important feature of OOP, in essence, is that pointers to base classes can point to objects of derived classes
One, static type and dynamic type
A static type is known at compile time, which is the type of the variable declaration, such as int s, String str, and the dynamic type is not known until run time, which is called dynamic binding. For example, the base class parent and its derived class son,
Son s;parent *p = &s; Here the static type of P is the parent *, and the true type of the bound object is son. therefore, when using a reference or pointer to a base class, we do not know the actual type of the pointer or the object to which the reference is bound. The object might be a base class, or it might be a derived class. (not determined until run time). However, for non-pointers, non-reference ordinary objects, their static type is always equal to the dynamic type.
Second, virtual function
Virtual functions are used to enable member functions in derived classes to override member functions in the base class.
A virtual function in a base class must be declared with the keyword, while the derived class is optional, and if the derived class is to overwrite a member function in the base class, add the override keyword to explicitly declare it.
Third, cover and hide
First, define a concept in which the scope of the derived class is within the scope of the base class, although they are detached from the code.
In the following code, the output () function in a derived class is not an override of a base class, it is simply a hiding of a base class function, as if a local variable of the same name is hiding a global variable. Within the scope of the derived class, it looks only at the output () function of the derived class; So if you declare an object of the Son Class S,s.output (), it will print out son. However, the parent *ps = &s;ps->output (), which prints out the parent;
Because output is not a virtual function, it cannot be overwritten, that is, it executes parent::output ().
classParent { Public: intA; voidoutput () {cout<<"Parent"<<Endl; }};classSon: PublicParent { Public: intb; voidoutput () {cout<<"son"<<Endl; }};
The following code implements the overwrite of the output ()
classParent { Public: intA; Virtual voidoutput () {cout<<"Parent"<<Endl; }};classSon: PublicParent { Public: intb; voidOutput ()Override{cout<<"son"<<Endl; }};
With the code above, we can make it call Son::output (), whether it's a normal son object or a parent pointer bound to a son object.
PS: For the pointer ps of a base class, PS cannot invoke a member unique to a derived class even if PS is bound to an object of a derived class.
Iv. abstract base class
Pure virtual function: We write this function in the base class, but do not want to define the body of his function, but want to overwrite him in the derived class. That is, this function has no practical meaning in the base class.
Abstract base class: A class that contains (or inherits directly from) a pure virtual function, as if it were the concept of an interface in Java. An abstract base class cannot instantiate an object.
"C + +" oop