-1

I'm having a scenario where i'm implementing multilevel inheritance but first level inheritance is specified as protected inheritance , but it is giving me compilation issue.

class A
{
 protected:
    int a1;
};

class B: protected A
{
 protected:
    int b1;
};

class C: public B 
{
public:
    C()
    {
        a1=10;
        b1=20;
        cout<<a1<<b1<<endl;
    }
};

int main()
{
    C c;   //can access A class protected data
    A* a= new C; //compilation error: cannot cast 'C'to its protected base class 'A'
}

My issue is when i do inheritance using protected access specified ,I'm able to access all A Class data members then why can't I'm able to have a pointer of A class type holding C object?

amateur17
  • 9
  • 3

1 Answers1

2

main is not privy to the inheritance relationship due to its protected status.

class B: protected A

is what makes it inaccessible outside of B:: and C::

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67