Study day 54



  1. Subclass object model

    1. Derived classes contain multiple components(multiple sub objects) the contents of multiple objects need not be related
    2. It contains the member variables defined by the subclass itself, the sub objects of the member functions
    3. The subclass inherits the subobjects of the base class, and the subobjects contain the member variables and member functions defined by the base class
    4. Subclass objects can be used as parent objects because derived class objects contain base class parts
    5. You can use the base class pointer to the new subclass objectCompiler implicitly converts from subclass to base classWhere the base class reference is used, it can be directly replaced by the reference of the subclass object
  2. Subclass constructor

    1. Subclass uses the constructor of the parent class to initialize its parent class part, the parent class controls the member function initialization of the parent class part, and the subclass is responsible for the initialization of the function members of the child class part
    2. Structure inheritance is public inheritance by default
    3. When the constructor is used, the constructor of the base class is executed first, and then the constructor of the subclass is executed, otherwise, the constructor of the subclass is released first, and then the constructor of the parent class is released
  3. That is, when the parent class is a subclass

    1. class a{  };
      Class B: public a {}: // A is the direct base class of B
      Class C: public B {}: // A is the indirect base class of C
    2. The inheritance relationship is always passed, forming the inheritance chain, and the final structure will be the members of the direct parent class that C contains and the members of each indirect parent class

    3. Direct base class

    4. Indirect base class

    5. B even if the parent class is a subclass

  4. Classes that are not parented

    1. The final keyword, placed after the class name, makes this class unable to become a base class
  5. Static type and dynamic type

    1. Static typeIt is the type of variable declarationStatic types compile with knownYes
    2. Dynamic typePointer or reference represents or represents the type of object in memoryIt’s only known at runtime
    3. The dynamic and static types are onlyBase class pointer or referenceIn other words, there is no dynamic type and static type
  6. Implicit type conversion from subclass to base class

    1. The compiler implicitly converts a subclass to a parent becauseEach derived class object contains a parent part
    2. Base class objects can exist independently or as part of subclass objects
    3. Automatic type conversion without parent class
    4. A reference to a subclass cannot be bound to a base class. Similarly, a pointer to a derived class cannot point to a parent class address
    5. If there is a virtual function in the parent class, you can use dynamin_ Cast conversion, subclass to parent
  7. Copy and assignment between parent class and derived class

    1. Deriving class objects to define and initialize base class objects will cause base class copy function execution
    2. Whether it is assignment or copy, it can only handle the members of the base class itself, not the members of the derived class, even the members of the derived class
    3. When a derived class object is used to initialize or assign a value to a base class object, only the base class part of the derived class object will be copied and assigned, and the derived part will be ignored
    4. The things of the parent class and the child class

Recommended Today

How does git store data

How does git store data Git is one of the tools that we use most every day. It is a new version control tool created by Linus Torvalds, the earliest author of Linux kernel. It is as simple and easy to use as Linux. This article will briefly talk about how git works requirement I […]