Public inheritance, private inheritance, and protection inheritance in C ++

Source: Internet
Author: User

Public inheritance, private inheritance, and protection inheritance in C ++
I. Reasons for the article

The simple and clear reason is a common problem that has not been solved. I want to clarify this problem.

Ii. Lengthy Definition

Let's take a look at these lengthy definitions:

Public inheritance:

When the class is inherited by the public, the access attributes of the public and protected members of the base class remain unchanged in the derived class, and Private Members of the base class cannot directly access it. That is to say, the base classPublic MemberAndProtect membersThe access attributes inherited from the derived class remain unchanged. They are still public and protected members of the derived class. Other members of the derived class can directly access them. In addition to the class family, you can only access the public members inherited from the base class through the objects of the derived class.

Private inheritance:

When the class inheritance method is private inheritance, both the public and protected members in the base class appear in the derived class as private members, private Members of the base class cannot be directly accessed in the derived class. That is to say, the public and protected members of the base class are inherited and used as Private Members of the derived class. Other members of the derived class can directly access them, however, you cannot directly access them outside the class family through the objects of the derived class. Neither a member of a derived class nor an object of A derived class can directly access a private member inherited from a base class.

Protection inheritance:

In Protection inheritance, both the public and protected members of the base class appear in the derived class as the protected member, and the Private member variables of the base class cannot be directly accessed. In this way, other members of the derived class can directly access the public and protected members inherited from the base class, but they cannot be directly accessed outside the class through the objects of the derived class, neither the member of the derived class nor the object of the derived class can directly access the private member of the base class.

3. Summarize the lengthy definition above

After reading the lengthy definition, it does not actually play a major role ~~ Because the definition is independent and rigorous, there are common things, and there is no induction or association between them.
Well, let's make the following summary:

Public inheritance:

1. four words:Unchanged

2. Permissions:
(1) The derived class members can only access the public/protected members in the base class;
(2) The object of the derived class can only access public members in the base class. (Note:The derived class and the derived class object are different)

Private inheritance:

1. four words:All changed to private

2. Permissions:
(1) The derived class members can only access the public/protected members in the base class;
(2) The object of the derived class cannot access any member of the base class.

Protection inheritance:

1. More words:Public, Change Protection

2. Permissions:
(1) A member of a derived class can only access public/protected members in the base class;
(2) The object of the derived class cannot access any member of the base class.

Commonalities:

1. The private end is private and inaccessible;
2. This is like a permission size inclusion and constraint relationship;
3. objects only have public inheritance and can access public members. Others cannot be accessed;
4. For the three types of inheritance, access by members is the same, because the base class members have been copied to the subclass by corresponding permission rules;
5. The members mentioned above can be:
(1) member functions
(2) member variables

I found that there are still too many images to better understand ~~

// The public inherits the [unchanged] Access Object of the member public --> public Y Yprotected --> protected Y Nprivate --> private N // The private inherited member accesses the object public --> private Y nprotected --> private Y Nprivate --> private N // protects the Access Object of the inherited member from public --> protected Y Nprotected --> protected Y Nprivate --> private N
Iv. Example

All things finally need to talk to instances.

Example 1 (for member functions ):

The following is a question for a software designer:

Three classes of O, P, and Q are known. Class O defines a private method F1, a public method F2, and a protected method F3; class P and class Q are derived classes of Class O. The Inheritance Method is as follows:
(1) class P: protected O {...}
(2) class Q: public O {...}

Which of the following statements about method F1 is true? (B ):
A. Method F1 cannot be accessed
B. Method F1 can be accessed only within Class O
C. Access Method F1 only within Class P
D. Access Method F1 only within Class Q

Which of the following statements about method F2 is true? (A): [the answer to this question is C. I personally think it is wrong]
A. objects like O, P, and Q can access the F2 method.
B. objects like P and Q can access the F2 method.
C. Objects of Class O and Q can access the F2 method.
D. Access Method F2 only within Class P

Which of the following statements about method F3 is true? (B ):
A. Objects of Class O, P, and Q can access method F3.
B. Objects of the Class O, P, and Q cannot access method F3. [do not hesitate to select]
C, Class O and Q objects can access method F3
D. objects like P and Q can access method F3.

Then, based on the above information, write the following code:

/*** Funtion: note that the following code cannot be accessed: ** Date: ** Author: Bill Wang */# include
  
   
Using namespace std; // class O {public: void F2 () {cout <
   
  

The running result is as follows:

The above questions and charts are fully verified ~~

Example 2 (for member variables ):

See the code for details.

# Include  Using namespace std; class A // parent class {private: int privatedateA; protected: int protecteddateA; public: int publicdateA;}; class B: public A // public inheritance {public: void funct () {int B; // B = privatedateA; // error: Private Members in the base class are invisible in the derived class B = protecteddateA; // OK: the protected member of the base class is the protected member B = publicdateA In the derived class; // OK: The Public Member of the base class is a public member in the derived class}; class C: private A // The derived class C of the base class A (private inheritance) {public: void funct () {int c; // c = privatedateA; // error: the private member in the base class is invisible in the derived class c = protecteddateA; // OK: the protected member of the base class is a private member c = publicdateA In the derived class; // OK: the public Member of the base class is A private member }}; class D: protected A // The derived class D of the base class A (protected inheritance) {public: void funct () {int d; // d = privatedateA; // error: Private Members in the base class are invisible in the derived class d = protecteddateA; // OK: the protected member of the base class is the protected member d = publicdateA In the derived class; // OK: The Public Member of the base class is the protected member in the derived class}; int main () {int; B objB; // a = objB. privatedateA; // error: Private Members in the base class are invisible to the derived class and invisible to the object. // a = objB. protecteddateA; // error: the protected member of the base class is a protected member in the derived class, And a = objB is invisible to the object. publicdateA; // OK: The Public Member of the base class is a public member in the derived class, visible to the object C objC; // a = objC. privatedateA; // error: Private Members in the base class are invisible to the derived class and invisible to the object. // a = objC. protecteddateA; // error: the protected member of the base class is a private member in the derived class and invisible to the object. // a = objC. publicdateA; // error: the Public Member of the base class is a private member in the derived class, and D objD is invisible to the object; // a = objD. privatedateA; // error: Private Members in the base class are invisible to the derived class and invisible to the object. // a = objD. protecteddateA; // error: the protected member of the base class is a protected member in the derived class and invisible to the object. // a = objD. publicdateA; // error: the Public Member of the base class is a protected member in the derived class, and return 0 is invisible to the object ;} 

I found another problem when writing this code. I will not go into details here. The content of an article should not be too rich, otherwise it will be easy to get lost, analyze in subsequent articles ~~~

Write it here ~~~

 

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.