Access Form of the derived class in C ++ to the base class member

Source: Internet
Author: User

Source: http://www.cppblog.com/woaidongmao/archive/2010/07/01/119024.html

In C ++, a derived class can access base class members in the following two forms:
1. Internal access: Access to members inherited from the base class by new members in the derived class.
2. Object Access: Access to members inherited from the base class through the object of the derived class outside the derived class. Today, we will introduce the access rules of the derived class on the base class members in the inheritance mode of 3.

 

1. Private inherited access rules
When the class inheritance method is private inheritance, the public and protected members of the base class are inherited and become private members of the derived class. Other members of the derived class can directly access them, however, the object of the derived class cannot be accessed outside the class. The private member of the base class cannot be directly accessed in the private derived class. Therefore, no matter whether it is a member of the derived class or an object of the derived class, you cannot directly access the private member inherited from the base class, however, it can be indirectly accessed through the Public member functions provided by the base class. The access rules of private inheritance are summarized as follows:

Base class member

Private member

Public Member

Protected Member

Internal access

Inaccessible

Accessible

Accessible

Object Access

Inaccessible

Inaccessible

Inaccessible

 

2. access rules inherited by the Public
when the class is inherited by the public, the public and protected members of the base class are inherited to the derived class and still serve as the public and protected members of the derived class. Other members of the derived class can directly access them. However, the external user of the class can only access the inherited public member through the object of the derived class. The private member of the base class cannot be directly accessed in the private derived class. Therefore, no matter whether it is a member of the derived class or an object of the derived class, you cannot directly access the private member inherited from the base class, however, you can directly access them through the Public member functions provided by the base class. The public-inherited access rules are summarized as follows:

Base class member

Private member

Public Member

Protected Member

Internal access

Inaccessible

Accessible

Accessible

Object Access

Inaccessible

Accessible

Inaccessible

 

3. Protection of inherited access rules
When the class inheritance method is to protect inheritance, the public and protected members of the base class are inherited to the derived class as the protected member of the derived class. Other members of the derived class can directly access them, however, the external users of the class cannot access them through the objects of the derived class. The private member of the base class cannot be directly accessed in the private derived class. Therefore, no matter whether it is a member of the derived class or an object of the derived class, you cannot directly access the private member of the base class. The inherited access protection rules are summarized as follows:
 

Base class member

Private member

Public Member

Protected Member

Internal access

Inaccessible

Accessible

Accessible

Object Access

Inaccessible

Inaccessible

Inaccessible

 

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.