-1

Here's my code:

class Base{
    public:
    getValue(int a){
        x = a;
    }
    protected:
        int x;
    };

class Derived: public Base{
public:
    Derived():Base(){
        Value = 0;
    }
    void Function();
}

So my confusion comes from the inheritance when you have to set the scope of the Base function in the derived function, in google it says that everything in the base class becomes public. If that is the case, doesn't the protected value in the Base function become easily accessible in the Derived function? isn't that bad?

If this is the case isn't there a way keep the protected value from the base class protected in the derived class.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 1
    *"..in google it says that everything in the base class becomes public.. "* - Its wrong. Don't always trust Google, there are so many wrong stuff on C++ out there. See [Difference between private, public, and protected inheritance](http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance?rq=1). – WhiZTiM Feb 23 '17 at 19:50
  • BTW, its not called *"scope"*, *"scope"* is about something else. Your subject is themed on [*"member access specification"*](http://en.cppreference.com/w/cpp/language/access) – WhiZTiM Feb 23 '17 at 19:58

1 Answers1

3

Public inheritance does not mean that everything becomes public. It means that what is public from Base becomes public in Derived. The protected data member, is not made public.

If you had used protected inheritance, then the public methods of Base would be made protected in Derived. Had you used private inheritance, then the public and protected methods in Base would be private in Derived.

I guess you can say that the inheritance encapsulation instructs the maximum visibility of the base class, not the absolute visibility of it.

Edit: Actually the link provided by WhiZTiM explains it quite nicely: Difference between private, public, and protected inheritance

Community
  • 1
  • 1
Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50