template boy
template boy

Reputation: 10480

Trouble with public/protected/private inheritance

I'm attempting a simple example of inheritance in C++. But I just can't get it down. When I try to get the protected members of class B inherited from class A it says that A::baz is protected.

#include <iostream>

class A {
    public:
        int foo;
        int bar;
    protected:
        int baz;
        int buzz;
    private:
        int privfoo;
        int privbar;
};

class B : protected A {}; // protected members go to class B, right?

int main() {
    B b;

    b.baz; // here is the error [A::baz is protected]
}

I can't seem to find what I'm doing wrong. I've tried changing class B : protected A to : public A but it still doesn't work.

Upvotes: 2

Views: 355

Answers (7)

Jon
Jon

Reputation: 437376

When a member is protected, it can only be accessed from within methods of the class that defines it and its descendants.

What you are trying to do is access the protected/private members from code external to these classes, which is not allowed. You can only access public members of a class from outside of class's scope.

Upvotes: 0

anthonyvd
anthonyvd

Reputation: 7590

You should read about public/private inheritance in C++. What you want to achieve is done by replacing

class B : protected A {};

by

class B : public A {};

EDIT: I read too fast and didn't notice you tried to access baz from main. You can only access it from a member method.

Upvotes: 0

Caesar
Caesar

Reputation: 9843

The protected access specifier is similar to private. Its only difference occurs in fact with inheritance. When a class inherits from another one, the members of the derived class can access the protected members inherited from the base class, but not its private members.

More on it here

Upvotes: 0

ruakh
ruakh

Reputation: 183311

Because A::baz is protected, B can access it:

class B : public A
{
  public:
    int some_other_method()
    {
        return baz;
    }
};

but that doesn't let other code access it.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258608

You can access protected members from inside a deriving class, not outside.

class B : protected A 
{
   void foo() 
   {
       int x = foo; //ok
       x = baz;     //ok
       x = privfoo; //error
   }
};

Inheritance type only limits base class access. If, for example, you choose protected inheritance, all public methods in A will become protected to the outside, and the rest remain the same.

Upvotes: 2

tletnes
tletnes

Reputation: 1998

protected fields can only be accessed by methods in the class declaring them, or classes inheriting from the declaring class. you are trying to access a protected field ffrom a global function.

Upvotes: 0

Doug T.
Doug T.

Reputation: 65609

Protected inheritance just impacts how clients of your class see the public interface of the base class. Protected inheritance marks the public members of the base class as protected for users of your inherited class.

So baz in your example is not public, it is protected from B, hence the compiler error.

Upvotes: 5

Related Questions