C + + inheritance three ways __c++

Source: Internet
Author: User
Tags inheritance visibility

1. Three types of class members

Public members can be accessed by any class,

Protected members are limited to themselves and subclass access. Used in the same class, whose access rights are equivalent to a private member, but when derived, his access rights are equivalent to that of a public member

Private members are limited to their own access, that is, they can only be used within their own classes.

2, for public inheritance mode:

• The visibility of the base class member to the derived class for the derived class,

The public and protected members of the base class are visible: Both the public members of the base class and the protected members as members of the derived class remain in their original state;

Private members of the base class are not visible: private members of the base class are still private, and derived classes cannot access private members in the base class.

Therefore, for a derived class object, the public members of the base class are visible and the other members are not visible. In public inheritance, the objects of the derived class can access public members in the base class, and the member functions of the derived class can access public and protected members in the base class. 3, for private inheritance way:

• The visibility of the base class member to the derived class for the derived class,

The public and protected members of the base class are visible: the public and protected members of the base class are both private members of the derived class and cannot be accessed by subclasses of this derived class;

The private members of the base class are not visible: derived classes cannot access private members in the base class.

Therefore, for a derived class object, all members of the base class are not visible. In private inheritance, the public and protected members of the base class can only be accessed by members of the directly derived class, not by objects, and cannot be inherited further down. 4. For the protection of inheritance mode:

• The visibility of the base class member to the derived class for the derived class,

The public and protected members of the base class are visible: the public and protected members of the base class are protected members of the derived class and can be accessed by other members, but not by the object of the derived class;

The private members of the base class are not visible: derived classes cannot access private members in the base class.

Therefore, for a derived class object, all members of the base class are not visible. When you protect inheritance, members of the base class can also be accessed only by other members of the directly derived class, not by objects, and cannot be inherited further down.

This type of inheritance is the same as the private inheritance approach. The difference between the two is only for the members of the derived class, but if the derived class continues to derive as the new base class, there is a distinct difference.

For example: Class B inherits the Class A in private, Class B derives new Class C, then other members of Class C and objects of derived classes cannot access members that are indirectly inherited from Class A (because they belong to a private member in Class B, and cannot access private members regardless of how the C class is derived)

If Class B inherits the Class A in a protected manner, Class B is derived from the new Class C (assuming it is inherited in public). Then other Members of Class C (except their objects) can access members that are indirectly inherited from Class A (because they belong to a protected member in Class B and derive the C class in the form of public inheritance). Just like a B-class public member can be accessed)

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.