Difference between private, public, and protected inheritance

后端 未结 16 1791
灰色年华
灰色年华 2020-11-21 06:15

What is the difference between public, private, and protected inheritance in C++?

相关标签:
16条回答
  • 2020-11-21 06:27

    Summary:

    • Private: no one can see it except for within the class
    • Protected: Private + derived classes can see it
    • Public: the world can see it

    When inheriting, you can (in some languages) change the protection type of a data member in certain direction, e.g. from protected to public.

    0 讨论(0)
  • 2020-11-21 06:28

    It has to do with how the public members of the base class are exposed from the derived class.

    • public -> base class's public members will be public (usually the default)
    • protected -> base class's public members will be protected
    • private -> base class's public members will be private

    As litb points out, public inheritance is traditional inheritance that you'll see in most programming languages. That is it models an "IS-A" relationship. Private inheritance, something AFAIK peculiar to C++, is an "IMPLEMENTED IN TERMS OF" relationship. That is you want to use the public interface in the derived class, but don't want the user of the derived class to have access to that interface. Many argue that in this case you should aggregate the base class, that is instead of having the base class as a private base, make in a member of derived in order to reuse base class's functionality.

    0 讨论(0)
  • 2020-11-21 06:30

    I have tried explaining inheritance using a picture below.

    The main gist is that the private members of parent class are never directly accessible from derived/child class but you can use parent class's member function to access the private members of parent class. Private variables are always present in derived class but it cannot be accessed by derived class. Its like its their but you cannot see with your own eyes but if you ask someone form the parent class then he can describe it to you.

    0 讨论(0)
  • 2020-11-21 06:32

    If you inherit publicly from another class, everybody knows you are inheriting and you can be used polymorphically by anyone through a base class pointer.

    If you inherit protectedly only your children classes will be able to use you polymorphically.

    If you inherit privately only yourself will be able to execute parent class methods.

    Which basically symbolizes the knowledge the rest of the classes have about your relationship with your parent class

    0 讨论(0)
  • 2020-11-21 06:34
    class A 
    {
    public:
        int x;
    protected:
        int y;
    private:
        int z;
    };
    
    class B : public A
    {
        // x is public
        // y is protected
        // z is not accessible from B
    };
    
    class C : protected A
    {
        // x is protected
        // y is protected
        // z is not accessible from C
    };
    
    class D : private A    // 'private' is default for classes
    {
        // x is private
        // y is private
        // z is not accessible from D
    };
    

    IMPORTANT NOTE: Classes B, C and D all contain the variables x, y and z. It is just question of access.

    About usage of protected and private inheritance you could read here.

    0 讨论(0)
  • 2020-11-21 06:35

    These three keywords are also used in a completely different context to specify the visibility inheritance model.

    This table gathers all of the possible combinations of the component declaration and inheritance model presenting the resulting access to the components when the subclass is completely defined.

    enter image description here

    The table above is interpreted in the following way (take a look at the first row):

    if a component is declared as public and its class is inherited as public the resulting access is public.

    An example:

     class Super {
        public:      int p;
        private:     int q;
        protected:   int r;
     };
    
     class Sub : private Super {};
    
     class Subsub : public Sub {};
    

    The resulting access for variables p, q, r in class Subsub is none.

    Another example:

    class Super {
        private:     int x;
        protected:   int y;
        public:      int z;
     };
    class Sub : protected Super {};
    

    The resulting access for variables y, z in class Sub is protected and for variable x is none.

    A more detailed example:

    class Super {
    private:
        int storage;
    public:
        void put(int val) { storage = val;  }
        int  get(void)    { return storage; }
    };
    int main(void) {
        Super object;
    
        object.put(100);
        object.put(object.get());
        cout << object.get() << endl;
        return 0;
    }
    

    Now lets define a subclass:

    class Sub : Super { };
    
    int main(void) {
        Sub object;
    
        object.put(100);
        object.put(object.get());
        cout << object.get() << endl;
        return 0;
    }
    

    The defined class named Sub which is a subclass of class named Super or that Sub class is derived from the Super class. The Sub class introduces neither new variables nor new functions. Does it mean that any object of the Sub class inherits all the traits after the Super class being in fact a copy of a Super class’ objects?

    No. It doesn’t.

    If we compile the following code, we will get nothing but compilation errors saying that put and get methods are inaccessible. Why?

    When we omit the visibility specifier, the compiler assumes that we are going to apply the so-called private inheritance. It means that all public superclass components turn into private access, private superclass components won't be accessible at all. It consequently means that you are not allowed to use the latter inside the subclass.

    We have to inform the compiler that we want to preserve the previously used access policy.

    class Sub : public Super { };
    

    Don’t be misled: it doesn’t mean that private components of the Super class (like the storage variable) will turn into public ones in a somewhat magical way. Private components will remain private, public will remain public.

    Objects of the Sub class may do "almost" the same things as their older siblings created from the Super class. "Almost" because the fact of being a subclass also means that the class lost access to the private components of the superclass. We cannot write a member function of the Sub class which would be able to directly manipulate the storage variable.

    This is a very serious restriction. Is there any workaround?

    Yes.

    The third access level is called protected. The keyword protected means that the component marked with it behaves like a public one when used by any of the subclasses and looks like a private one to the rest of the world. -- This is true only for the publicly inherited classes (like the Super class in our example) --

    class Super {
    protected:
        int storage;
    public:
        void put(int val) { storage = val;  }
        int  get(void)    { return storage; }
    };
    
    class Sub : public Super {
    public:
        void print(void) {cout << "storage = " << storage;}
    };
    
    int main(void) {
        Sub object;
    
        object.put(100);
        object.put(object.get() + 1);
        object.print();
        return 0;
    }
    

    As you see in the example code we a new functionality to the Sub class and it does one important thing: it accesses the storage variable from the Super class.

    It wouldn’t be possible if the variable was declared as private. In the main function scope the variable remains hidden anyway so if you write anything like:

    object.storage = 0;
    

    The compiler will inform you that it is an error: 'int Super::storage' is protected.

    Finally, the last program will produce the following output:

    storage = 101
    
    0 讨论(0)
提交回复
热议问题