2
#include<iostream>
using namespace std;
class Base{
    private:
        int a;
    protected:
        Base(){};
        virtual ~Base(){};
};
class Derived:private Base{
    private:
        int b;
    public:
        Derived(){};
        ~Derived(){};
        void test(){
            Base world;
        };

};

int main(){
}

inherit.cc|7 col 3| error: ‘Base::Base()’ is protected
inherit.cc|17 col 9| error: within this context
inherit.cc|8 col 11| error: ‘virtual Base::~Base()’ is protected
inherit.cc|17 col 9| error: within this context

But why?

And why this is correct?

#include<iostream>
using namespace std;
class Base{
    private:
        int a;
    protected:
        Base(){};
        virtual ~Base(){};
        void test2(){};
};
class Derived:private Base{
    private:
        int b;
    public:
        Derived(){};
        ~Derived(){};
        void test(){
            test2();
        };

};

int main(){
}
bitstore
  • 326
  • 2
  • 10
  • possible duplicate of [One question about protected constructor](http://stackoverflow.com/questions/2393325/one-question-about-protected-constructor) – Bo Persson Feb 25 '12 at 10:42

3 Answers3

2

In the first example you create an object of Base in method test() of Derived class.

In second example you access the method test2() which is protected in Base & Derived derives from it privately.

Note the rule for access specifiers of a class:

Protected Access specifier means that the members declared as Protected are accessible from outside the class BUT only in a class derived from it.

In case of private Inheritance:

All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.

In example 1,
Eventhough, Derived derives from Base, it can have access to protected members of Base only for the Base of the Derived object whose member function(test()) is being called not any other Base class object.
Given that you cannot create a Base object.

In example 2,
test2() is called on the object whose member function(test()) is being called, As noted above Derived class has access to protected members of Base for the Base of the Derived object whose member function is being called and hence test2() can be called.

Good Read:
What are access specifiers? Should I inherit with private, protected or public?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @tinybit: Please read the explanation carefully, *Eventhough, Derived derives from Base, **it can have access to protected members of Base only for the Base of the Derived object whose member function(test()) is being called not any other Base class object.*** – Alok Save Feb 25 '12 at 11:12
0
#include<iostream>
using namespace std;
class Base{
    private:
        int a;
    protected:
        Base(){}
        Base(int a) : a(a) {}
        virtual ~Base(){}
        void test2(){}
};

class Derived:private Base{
    private:
        int b;
        Derived(int b) : Base(b) {};
    public:
        Derived() : Base() {}; // uses protected constructor
        ~Derived(){};
        Base* test(){
            new Derived(-1); // uses the other protected constructor
        };

};
paulocuneo
  • 99
  • 1
  • 3
0

In void Derived::test() you try to create instance of Base class. Because of nothing said about arguments - it try to call Base default constructor, but it's protected - not public.

Also at the end of Derived::test() will be called destructor, but it also protected. So you cannot create instance of Base class with such modifiers for constructor and destructor

Olympian
  • 758
  • 1
  • 6
  • 13