C++ Inheritance and Static Members of Classes

What is inheritance?

Inheritance (inheritance) mechanism is the most important means to make code reusable in object-oriented programming, which allows programmers to extend and add functions on the basis of maintaining the original class characteristics. The new class generated in this way is called a derived class (or subclass), and the inherited class is called a base class (or parent class).

The vernacular means: There are two classes, A and B. Suppose class B inherits class A. Then class B can be expanded on the basis of class A to increase functions. Then class B is the subclass (derived class) at this time, and class A is the parent class (base class) of class B.

So after inheritance, can class B inherit all members and member functions of class A? We're about to talk about inheritance rules.

C++ inheritance rules:

There are three types of inheritance: public: protected: private:

class B :public A

class B :protected A

class B :private A

If it is public inheritance: when inheriting, the attributes of each member in the base class are kept unchanged, and the private members in the base class are hidden. Members of derived classes can only access public/protected members of the base class, but not private members; objects of derived classes can only access public members of the base class.

If it is protected inheritance: when inheriting, each member attribute in the base class becomes private, and the private members in the base class are hidden. Members of derived classes can only access public/ protected members of the base class, but not private members; objects of derived classes cannot access any members of the base class.

If it is private: Inheritance: When inheriting, each member attribute in the base class becomes private, and the private members in the base class are hidden. Members of derived classes can only access public/ protected members of the base class, but not private members; objects of derived classes cannot access any members of the base class.

Definition of inheritance: 

 insert image description here

1. The private members of the base class are not visible to the derived class no matter how they are inherited. Invisibility here means that the private members of the base class are still inherited into the derived class object, but the derived class object is grammatically restricted from being able to access it no matter inside or outside the class.
2. The private members of the base class cannot be accessed in the derived class. If the base class member does not want to be accessed directly outside the derived class, but needs to be accessed in the derived class, it is defined as protected. It can be seen that the protected member qualifier is due to inheritance.
3. The private members of the base class are not visible in the subclass; the access method of other members of the base class in the subclass is the one with less permission in the access qualifier and inheritance method (permission sorting: public>protected>private).
4. When using the keyword class, the default inheritance method is private, and when using struct, the default inheritance method is public, but it is best to write the inheritance method explicitly.
5. In actual use, public inheritance is generally used. Protected/private inheritance is rarely used, and the use of protected/private inheritance is not advocated, because protected/private inherited members can only be used in derived classes. And maintenance is not strong.

Static members of the class:


    Review: Static static in C
        Function 1: Modified variables are stored in the static storage area and only initialized once
        Function 2: Limit scope extension extern
    
    ordinary members:  
        member variables: each object of the class has its own member variables
        member function: class Each object can access
    the static members of the member function class through the object name:
        belongs to the class does not belong to the object!    
        Static member variable: All objects of the class share the same static member variable.
            It can only be initialized outside the class, not with a constructor.
Example: man, woman
    Every married woman has her own husband Ordinary member
    Static member There is only one man in the world who is the husband of all women

    Static member functions cannot access non-static member variables.
    Static member functions can be called not only by object name, but also by class name.

Guess you like

Origin blog.csdn.net/makabaka12138/article/details/128584417