What exactly is protected inheritance? I know about private and public inheritance, but not protected.
What exactly is protected inheritance? I know about private and public inheritance, but not protected.
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; }
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; }
What he said was true, but it's not protected inheritance but protected attribute.Originally posted by Polymorphic OOP
Nonono, that's not true at all!
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
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.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.
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.
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.
Seems everyone here is right...your just talking about different things...
That's not private and protected inheritance -- that's inheriting from classes that have private and protected members. There's a big difference.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.
Thanx Shiro and Poly. Very well explained.