Chapter 4 Object-Oriented Programming (4)

Source: Internet
Author: User

15.2.5 public, private, and protected inheritance
Access to the inherited members of a class is jointly controlled by the Access Level of the members in the base class and the access label used in the derived list of the derived classes.
Each class controls the access of members it defines. A derived class can further restrict access to inherited members.
A derived class cannot access the private Members of the base class, nor allow its users to access those members. If the base class member is public or protected, the access label used in the derived list determines the access level of the member in the derived class.
If it is a public inheritance, the base class members maintain their own access level: the public Member of the base class is the public Member of the derived class, and the protected member of the base class is the protected member of the derived class.
For protected inheritance (protected inheritance), the public and protected members of the base class are protected members in the derived class.
For private inheritance, all the members of the base class are private members in the derived class.

Class Base {
Protected:
Int I;
};
 
Class Child1: private Base {
Public:
Int GetValue (){
Return I;
}
};
No matter what access label is in the derived list, all the classes that inherit the Base have the same access to the Members in the Base. The derived access label controls the access of the derived class users to the members inherited from the Base.
The derived access label also controls access from a non-direct derived class.
1. interface inheritance and implementation inheritance
The public derived class inherits the interface of the base class, which has the same interface as the base class. In a well-designed class hierarchy, the object of the public derived class can be used in any place where the base class object is required.
Classes derived from private or protected do not inherit the interface of the base class. On the contrary, these derivatives are generally called implementation inheritance. In the implementation of a derived class, the inherited class does not become part of its interface.
So far, the most common inheritance form is public.
2. Remove individual members
If private or protected is inherited, the access level of the base class members is more limited than that of the base class.
A derived class can restore the access level of an inherited member, but it cannot make the access level more strict or loose than the original one specified in the base class.

Class Base {
Protected:
Int I;
};
 
Class Child1: private Base {
Protected:
Using Base: I;
Public:
Int GetValue (){
Return I;
}
};
Just as you can use using to declare a name from the namespace, or you can use using to declare a name in the access base class, except for replacing the namespace name with the class name on the left of the scope operator, the format is the same.
3. Default inheritance Protection Level
The classes defined by the reserved words of struct and class have different default access levels. Similarly, the default inherited access levels define different Derived classes based on the reserved words used. A derived class defined by the class reserved word has private inheritance by default, while a class defined by the struct reserved word has public inheritance by default.
The only difference between a struct reserved word-defined class and a class-defined class is the default Member Protection Level and the default derived protection level.
Although private inheritance is always default when class reserved words are used, this is rare in practice. Because private inheritance is so rare, it is usually better to explicitly specify private than default by default. Explicitly specify whether you want private inheritance rather than temporary negligence.

From xufei96's column

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.