difference between availability and accessiblity in inheritance

Viewed 53

I have a small doubt regarding this MCQ question on inheritance. I hope someone can help me with it.

mcq

The correct answer is c and i get it but my doubt is:

The option b states Private members of parent class are available in child class

and in inhertance Private members of parent class are not accessible in child class

so is availability and accessiblity different things in this context?? cause if its available in child class as private members then why can't i access them in the child class itself?

So can this also be the answer? only single option was clickable and no 4th option like both b and c were given.

1 Answers

Availability concerns name lookup. A derived class can find it's base class' private members even if it can't do anything with them.

Consider the following case of multiple inheritance. derived inherits from two base classes which each has a int x; member, one private and the other public (https://godbolt.org/z/fPKjKczzb) :

class base1
{
private:
    int x;
};

class base2
{
public:
    int x;
};

class derived : public base1, public base2
{
    void func()
    {
        // error: reference to 'x' is ambiguous
        x = 1;
    }
};

Here derived has 2 x; members available even if it can only access one. Therefore x = 1; is actually ambiguous. It seems intuitive that x = 1; should refer to base2::x but that isn't how name lookup rules are defined.

In my opinion, the linked question is poorly written. Even if you know the difference between availability and accessibility, which is rather obscure, it's easy to fall for the tricky wording. It seems designed to trick as many people as possible and doesn't seem to be of any value in evaluating actual knowledge or competence.

Related