Discussion on public inheritance, private inheritance and protection inheritance in C + +

Source: Internet
Author: User
Tags access properties

First, the article for the reason

Simple and clear reasons, commonplace but not really solve the problem, want to understand this problem.

Ii. a lengthy definition

Let's take a look at these lengthy definitions:

Public inheritance:

When a class inherits from a public inheritance, the access properties of the base class's public and protected members are not changed in the derived class, and the private members of the base class are not directly accessible. This means that the public and protected members of the base class are inherited into the derived class, and the Access property remains the same as the public and protected members of the derived class, and other members of the derived class can access them directly. Public members that inherit from a base class can be accessed only by the objects of the derived class outside the class family.

Private inheritance:

When a class inherits from a private inheritance, the public and protected members of the base class appear in the derived class as private members, and the private members of the base class are not directly accessible in the derived class. This means that the public and protected members of the base class are inherited as private members of the derived class, and other members of the derived class can access them directly, but objects outside the class family through derived classes cannot access them directly. Private members that inherit from a base class cannot be accessed directly, either by a member of a derived class or by an object of a derived class.

Protection inheritance:

In protection inheritance, the public and protected members of the base class appear in the derived class as protected members, and the private member variables of the base class are not directly accessible. In this way, other members of the derived class can directly access public and protected members inherited from the base class, but objects outside the class that are derived from the class cannot access them directly, neither the members of the derived class nor the objects of the derived class directly access the private members of the base class.

Iii. summarize the definition of the above lengthy

After reading the long definition, in fact, there is nothing too much effect ~ ~ Because the definition is strictly prohibited, there are common things, not to be summed up and hold the link
OK, let's make the following generalizations:

Public inheritance:

1. Four words: keep the original

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

Private inheritance:

1, or four words: all become private

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

Protection inheritance:

1, the word a little more: public, protection and protection

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

Common:

1, the private end is private, and inaccessible;
2, this is like a permission size contains, constraints relations, carefully experience;
3, the object only the public inheritance, you can access the members, the rest are inaccessible;
4, three kinds of inheritance, member access is the same, because the equivalent of the base class member is the corresponding permission rules are copied to the subclass;
5, the above mentioned members can be:
(1) member functions
(2) member variables

Found that there are some too many, draw a picture more understand ~ ~

//Public inheritance "immutable" member access to object access Public- PublicY yprotected-protectedY NPrivate-Privaten N//Private inherited member access to object access Public-PrivateY Nprotected-PrivateY NPrivate-Privaten N//Protect inherited members from accessing object access Public-protectedY Nprotected-protectedY NPrivate-Privaten N
Iv. examples

What is the last thing to talk about in an instance?

Example 1 (for member functions):

Here is a software designer exam question:

Known 3 classes O, p, Q, 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, and are inherited as follows:
(1) class p:protected o{...}
(2) class Q:public o{...}

A description of the method F1 is correct (B):
A, method F1 cannot be accessed
B. Methods can only be accessed within class O F1
C. Methods can only be accessed within class P F1
D. The method can only be accessed within class Q F1

The correct description of the method F2 is (A): "The answer to the question on the Internet is C, the individual feels wrong."
A, class O, P, Q objects can access the method F2
B, Class P, and Q objects can all access the method F2
C, Class O and Q objects can access the method F2
D. Methods can only be accessed within class P F2

A description of the method F3 is correct (B):
A, Class O, P, Q objects can access the method F3
B, Class O, P, Q of the object can not access the method F3 "can not hesitate to choose"
C, Class O and Q objects can access the method F3
The objects of the D, class p, and Q can All access the method F3

Then, according to the above information, write the following code:

/* * * funtion: Note that the following annotated code is not accessible * * date:2015-7-29* * Author:bill wang*/#include <iostream>using namespace STD;three functions with different access types defined in class Oclasso{ Public:voidF2 () {cout<<"This was O ' s public function F2"<<endl; }Private:voidF1 ();protected:voidF3 ();};//class P protection inherits from OclassP:protectedo{ Public:/* void test_f1 () {F1 (); }    */    voidTest_f2 () {F2 (); }voidTest_f3 () {F3 (); }};//Class Q public inherits from OclassQ: Publico{ Public:/* void test_f1 () {F1 (); }    */    voidTest_f2 () {F2 (); }voidTest_f3 () {F3 (); }};//Class R private inherits from OclassR:Privateo{ Public:/* void test_f1 () {F1 (); }    */    voidTest_f2 () {F2 (); }voidTest_f3 () {F3 (); }};//defined in the body of the class or outside the bodyvoidO::f1 () {cout<<"This is O ' s private function F1"<<endl;}voidO::f3 () {cout<<"This is O ' s protected function F3"<<endl;}//Main functionintMain () {o O; P p;//Protection inheritanceQ q;//Public inheritanceR R;//Private inheritance    cout<<"O:"<<endl;//o.f1 ();//Private MethodO.f2 ();//Public method    //o.f3 ();//Protection Method    cout<<endl;//Three kinds of inheritance: Same as member access    cout<<"P:"<<endl;//P.TEST_F1 ();//Private method cannot accessP.test_f2 (); P.TEST_F3 ();//p.f1 ();    //p.f2 ();    //p.f3 ();    cout<<endl;cout<<"Q:"<<endl;//Q.test_f1 ();Q.test_f2 (); Q.TEST_F3 ();//q.f1 ();Q.f2 ();//q.f3 ();    cout<<endl;cout<<"R:"<<endl;//R.test_f1 ();R.test_f2 (); R.TEST_F3 ();//r.f1 ();    //r.f2 ();    //r.f3 ();    return 0;}

The results of the operation are as follows:

Fully confirms the above topic and chart ~ ~

Example 2 (for member variables):

Is the same, see the code

#include <iostream>usingNamespace Std;class A//Parent class{Private:intPrivatedatea;protected:intProtecteddatea; Public:intPublicdatea;}; Class B: PublicA//Public Inheritance { Public:void funct()    {intb//b=privatedatea; Error: A private member in a base class is not visible in a derived classB=protecteddatea;//ok: A protected member of a base class is a protected member in a derived classB=publicdatea;//ok: A public member of a base class is a public member in a derived class}};class C:PrivateDerived class C for a//base class A (private inheritance) { Public:void funct()    {intC//c=privatedatea; Error: A private member in a base class is not visible in a derived classC=protecteddatea;//ok: A protected member of a base class is a private member in a derived classC=publicdatea;//ok: A public member of a base class is a private member in a derived class}};class D:protectedDerived class D for a//base class A (protection inheritance) { Public:void funct()    {intD//d=privatedatea; Error: A private member in a base class is not visible in a derived classD=protecteddatea;//ok: A protected member of a base class is a protected member in a derived classD=publicdatea;//ok: A public member of a base class is a protected member in a derived class}};intMain () {intA B OBJB;//a=objb.privatedatea; Error: A private member in a base class is not visible in a derived class and is invisible to an object    //a=objb.protecteddatea;//error: A protected member of a base class is a protected member in a derived class and is not visible to an objectA=objb.publicdatea;//ok: A public member of a base class is a public member in a derived class and is visible to an objectC OBJC;//a=objc.privatedatea; Error: A private member in a base class is not visible in a derived class and is invisible to an object    //a=objc.protecteddatea;//error: A protected member of a base class is a private member in a derived class and is not visible to an object    //a=objc.publicdatea; Error: The public member of the base class is a private member in the derived class and is not visible to the objectD OBJD;//a=objd.privatedatea; Error: A private member in a base class is not visible in a derived class and is invisible to an object    //a=objd.protecteddatea;//error: A protected member of a base class is a protected member in a derived class and is not visible to an object    //a=objd.publicdatea; Error: The public member of the base class is a protected member in a derived class and is not visible to the object    return 0;}

I wrote this code when the discovery of another problem, specifically not here, the content of an article is not too rich, otherwise easy to lose direction, in the following article analysis ~ ~ ~

Just write it down here ~ ~ ~

-end-

Reference documents

[1]http://wenku.baidu.com/link?url=is4t_0j4ty4-dfymmwnlp_ Kx5zgnbnfmqw5n9if-pejvd1qprcf-tmz7g7jhepdrinxaho9p5yv2ctkctvjdz0tlhglstatehx5xomcmds7

Copyright NOTICE: Welcome Reprint, note The source is good! If you do not like please leave a message to explain why again step on Oh, thank you, I can also know the reason, continuous progress!!

Discussion on public inheritance, private inheritance and protection inheritance in C + +

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.