1

Possible Duplicate:
What are access specifiers? Should I inherit with private, protected or public?

How can I create a derived class in C++ which preserves the properties and methods' access specifier like this:

class Base
{
private:
    void base_private();

protected:
    void base_protected();

public:
    void base_public();
};

class A: [what type is appropriate here?] Base
{
public:
    void test() {
        base_protected(); // Ok
    }
};

class B: [what type is appropriate here?] A
{
public:
    void test() {
        base_protected(); // Ok
    }
};

int main()
{
    A a;
    B b;

    a.base_public();        // Ok
    a.base_protected();     // Not Ok
    b.base_protected();     // Not Ok
    b.test();               // Ok

    return 0;
}

I mean base_protected() method is still protected in derived classes but base_public() is public.

Community
  • 1
  • 1
Bình Nguyên
  • 2,252
  • 6
  • 32
  • 47
  • 5
    That's what standard `public` inheritance does. – Bo Persson Dec 29 '12 at 14:16
  • @BoPersson: So it's not `public` inheritance make all methods in `protected` of base class to `public`? – Bình Nguyên Dec 29 '12 at 14:18
  • @BìnhNguyên: You need to read and understand the marked duplicate. It should answer all your questions. – Alok Save Dec 29 '12 at 14:19
  • 2
    @Binh - No, it sets a limit on the visibility. If you use protected inheritance, the inherited names will be protected or private. With private inheritance, everything will be private. With public inheritance, everything stays the way it is in the base class. – Bo Persson Dec 29 '12 at 14:22

2 Answers2

2

Lets consider the availability of members of Base:

  • base_private is not available to clients but available to Base itself
  • base_protected is not available to clients but available to Base itself
  • base_public is available to both clients and Base

Now, if you have class A : public Base (public inheritance), the availability of the members of Base will be:

  • base_private is not available to clients and not available to A itself
  • base_protected is not available to clients but available to A itself
  • base_public is available to both clients and A

Now, what you're asking about is how to keep the same client interface for both classes, Base and A. If you look at the availability of the members for clients in the lists above, you will see that it is precisely the same for Base and A: base_private is not available; base_protected is not available; and base_public is available.

The only thing that has changed between Base and A is that A cannot access the members that are private to Base. That's the whole point of the protected access control - it gives derived classes access to their base classes members without making them available to clients.

So public inheritance is what you need.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

A derived class can access public and protected members of a base class, no matter whether you inherit private, protected or public.

When you inherit private all members of the base class are considered private for derived classes of A. Similar for protected inheritance, derived classes of A may access protected and public members of Base.

In both cases, private and protected inheritance, the members of Base are not accessible to the "public".

And now to answer your question, use public inheritance to preserve the access specifiers of the base class.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198