Thread: Protected Inheritance

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    Protected Inheritance

    What exactly is protected inheritance? I know about private and public inheritance, but not protected.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    When you derive a class from one that has private members, there is no way to access those variables! So the protected: attribute allows subclasses to access members of the parent class that would otherwise be private. It is like an extended form of the private: trait.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You misread my post Poly. The protected attribute is really no different than private *except* that subclasses can access the data/functions. So private is for data/functions that are to be inaccessible from anywhere but that one class, protected is for data/functions you want inaccessible from anywhere but the class and it's children.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Polymorphic OOP
    Nonono, that's not true at all!
    What he said was true, but it's not protected inheritance but protected attribute.

    Code:
    //Protected attribute:
    class A
    {
      protected:
        int a;
    };
    
    //Protecte inheritance:
    class B:
      protected A
    {
       ...
    };

    (EDIT) Post order seems quite messed up...
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Sebastiani
    When you derive a class from one that has private members, there is no way to access those variables! So the protected: attribute allows subclasses to access members of the parent class that would otherwise be private. It is like an extended form of the private: trait.
    Nonono, that's not true at all! You can NOT access the private members of the class through private or protected inheritance just as you can't with public inheritance. On the contrary, protected and private inheritance force more restrictions.

    Private inheritance makes it as though the public and protected members of class you derived it from were private.

    Protected inheritance makes it as though the public members of the class it was derived from were protected.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Polymorphic OOP is correct, private members can never be accessed by use of inheritance. Only friends can access those private members.

    Note that protected inheritance of a class is not the same as having protected members in a class.

    Public inheritance: A class B derived from class A can use the public and protected members of class A.

    Protected inheritance: A class B derived from class A can use the pubilc and protected members of class A, but note that the public members of class A are protected in class B. This means that a class using class B, cannot use the public members of class A for B.

    Private inheritance: A class B derived from class A can use the pubilc and protected members of class A, but note that both public and protected members of class A are private in B. So a class C derived from B cannot use the public and protected members of class A.

    [edit]
    Imagine what would happen if by use of some kind of inheritance it would be possible to access private datamembers of a class, then the whole concept of encapsulation would be useless. Just inherit a class in that certain way and you would be able to access those private datamembers.
    [/edit]
    Last edited by Shiro; 12-27-2002 at 03:33 AM.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    29
    Seems everyone here is right...your just talking about different things...

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Sebastiani
    You misread my post Poly. The protected attribute is really no different than private *except* that subclasses can access the data/functions. So private is for data/functions that are to be inaccessible from anywhere but that one class, protected is for data/functions you want inaccessible from anywhere but the class and it's children.
    That's not private and protected inheritance -- that's inheriting from classes that have private and protected members. There's a big difference.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Thanx Shiro and Poly. Very well explained.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Inheritance in c++
    By Non@pp in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2006, 07:24 PM
  3. Trying to break inheritance
    By skorman00 in forum C++ Programming
    Replies: 9
    Last Post: 11-03-2005, 09:18 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. help with inheritance!
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 08:08 AM