C + + Learning two-inheritance

Source: Internet
Author: User
Tags access properties

Reprinted from HTTPS://WWW.CNBLOGS.COM/33DEBUG/P/6666939.HTML1, inheritance and derivation

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, it is much easier to inherit a property than from scratch, the primitive class is called the base class, and the inheriting class is called the derived class, which is similar to the father and son relationship, so it is also called the parent class and the child class. The subclass can also be inherited by another class as a parent class. There are three ways of inheriting (public), protection Inheritance (protect), and private inheritance.
The definition format is as follows:

2. Inheritance mode and Access properties

(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 of 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 class Base//Parent Class 2 {3 private:4 int _prib; 5 protected:6 int _prob; 7 Public:8 int _pubb;       9}; class Derived:public base//subclass, inherit from base, inherit type is public inheritance one by one {private:13 int _d_pri;14 protected:15       int _d_pro;16 public:17 void funct () + {int d;20 d=_prib;       Error: The private member in the base class is not visible in the derived class. D=_prob;       OK: The protected members of the base class are protected members in the derived class by D=_pubb; OK: The public member of the base class is a public member in the derived class at}24 int _d_pub;25};
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.
Class C:p rivate base//base class base C (Private inheritance), {public:29 void funct () ()--{c;32 C=_pri B Error: The private member in the base class is not visible in the derived class. C=_prob; OK: The protected members of the base class are C=_pubb for private members in derived classes; OK: The public member of the base class is a private member in the derived class}36};37 class E:p rotected base//base class base derived classes E (Protection inheritance). {public:40 void funct () 41 {E=_prib int e; Error: The private member in the base class is not visible in the derived class of the 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 46}47};
Summarize:
(2). The private member of the base class is not accessible in the 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 a 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.
A. Int. int main () () {n     . A;     D d;54     a=d._prib;     Error: A private member of a public-inherited base class is invisible in a derived class and is not visible to the object     A=d._prob;     Error: The protected member of the public inheritance base class is a protected member in the derived class, and the object is not visible in the     A=d._pubb.     OK: The public member of the common inherited base class is a public member in the derived class, and the object is visible in     c;59     a=c._prib;    Error: Private member of a private inheriting base class is invisible in the derived class and is not visible 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     A=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     e;64 E-     a=e._prib;    Error: Protection of private members in the inherited base class is not visible in the derived class, the object is invisible to the     A=e._prob;    Error: Protect the protected member of the inheriting base class in the derived class as a protected member, not visible to the object     A=e._pubb;    Error: Protect the public members of the inherited base class from being protected members in derived classes, not visible to  objects     0;69}

(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 is public when using a struct, but it is best to explicitly write the inheritance method.
(6). In the actual use of the general 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

C + + Learning two-inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.