Inheritance is an important means to make code reusable, and it is also one of the core ideas of object-oriented programming. Simply put, inheritance refers to the properties and methods of an object using another object directly. Inheritance presents the hierarchical structure of object-oriented programming, which embodies the cognitive process from simple to complex. The inheritance relationship in C + + is like a real-life parent-child relationship, inheriting a fortune is much easier than starting from scratch, the primitive class is called the base class, the inheriting class is called the subclass, they are similar to the father and the son's relationship, so also called the parent class and the subclass. There are three ways of inheriting (public), protection Inheritance (protect), and private inheritance.
The definition format is as follows:
1. Public inheritance
Public inheritance is characterized by the public and protected members of the base class as members of the derived class, which remain in their original state, while the private members of the base class are still private and cannot be accessed by subclasses of the derived class.
2. Private inheritance
Private inheritance is characterized by the public and protected members of the base class as private members of the derived class and cannot be accessed by subclasses of the derived class.
3. Protection Inheritance (protected)
The feature of protection inheritance is that all public and protected members of the base class become protected members of the derived class and can only be accessed by its derived class member functions or friends, and the private members of the base class are still private.
Private is able to keep the external and subclass private, except that the class in which the member resides can be accessed without any direct access. Protected is capable of external secrecy, but allows subclasses to access these members directly. The degree to which public, private, and protected protect member data or member functions can be described in the following table: a chestnut:
1 classBase//Parent Class2 {3 Private: 4 int_prib;5 protected:6 int_prob;7 Public:8 int_pubb;9 } ;Ten classDerived: PublicBase//subclass, inherited from base, inherited type is public inherit One { A Private: - int_d_pri; - protected: the int_d_pro; - Public: - voidfunct () - { + intD; -D=_prib;//Error: A private member in a base class is not visible in a derived class +D=_prob;//OK: The protected members of the base class are protected members in the derived class AD=_pubb;//OK: The public members of the base class are public members in the derived class at } - int_d_pub; - } ;
Summary: 1. Public inheritance is an interface inheritance that maintains the is-a principle, and the members that are available to each parent class are also available to the subclass, because each child class object is also a parent class object.
- classE |PrivateA//derived class C for base class base (private inheritance) - { - Public: in voidfunct () - { to intC; +C=_prib;//Error: A private member in a base class is not visible in a derived class -C=_prob;//OK: A protected member of a base class is a private member in a derived class theC=_pubb;//OK: The public member of the base class is a private member in the derived class * } $ };Panax Notoginseng classC |protectedA//derived class E (Protection inheritance) for base class base - { the Public: + voidfunct () A { the int e ; +E=_prib;//Error: A private member in a base class is not visible in a derived class -E=_prob;//OK: The protected members of the base class are protected members in the derived class $E=_pubb;//OK: The public members of the base class are protected members in the derived class $ } - };
Summarize:
2. A private member of a base class cannot be accessed in a derived class, and is defined as protected if the base class member does not want to be accessed directly outside the class, but needs to be accessible in the derived class. You can see that the protection member qualifier occurs because of inheritance.
3. Protected/private inheritance is an implementation inheritance, some members of the base class are not completely part of the subclass interface, and are the principle of has-a relationships, so the two inheritance relationships are not used under special circumstances, and are used in most scenarios as public inheritance. Private inheritance thinks this is-implemented-in-terms-of (is based on ...). Implementation). is usually lower than the combination (composition), but it is reasonable when a derived class needs to access the base class protection member or need to redefine the virtual function of the base class.
the intMain () - {Wuyi intA; - d D; WuA=d._prib;//Error: A private member of a public inheritance base class is not visible in a derived class and is invisible to an object -A=d._prob;//Error: The protected member of the public inheritance base class is protected in the derived class and is not visible to the object AboutA=d._pubb;//OK: Public members of the common inherited base class are public members in derived classes, visible to Objects - b b; -A=c._prib;//Error: Private member of a private inherited base class is not visible in a derived class and is invisible to the object -A=c._prob;//Error: The protected member of the private inheritance base class is a private member in the derived class and is not visible to the object AA=c._pubb;//Error: The public member of the private inheritance base class is a private member in the derived class and is not visible to the object the c C; -A=e._prib;//Error: Protection of private members in inherited base classes is not visible in derived classes and is invisible to objects $A=e._prob;//error: Protecting a protected member of an inherited base class is a protected member in a derived class and is not visible to an object theA=e._pubb;//Error: Protect the public members of the inherited base class from being protected members in derived classes, not visible to objects the the return 0; the}
4. Regardless of the inheritance method, the public and protected members of the base class can be accessed inside the derived class, and the private members of the base class exist but are not visible in the subclass (inaccessible).
5. The default inheritance method when using the keyword class is private, and the default inheritance method when using a struct is public, but it is best to show the inheritance in the way it is written.
6. In the actual application of the general use is the public inheritance, very few scenarios will use Protetced/private inheritance.
In struct inheritance, if no inheritance type is explicitly given, the default is public inheritance, and in class inheritance, the default is private if no inheritance type is explicitly given;
Inheritance Relationships & Access Qualifiers
Class inheritance in C + + (1) three ways to inherit