3

I understand everything what is stated here, it describes how Child accesses Parent members. But, how about Parent Accessing Child? I just can't understand this. Why it is wrong? Could you please explain in terms of static binding rules during compile time? Here Student class itself becomes protected I guess, but why?

using namespace std;
class Person
{
public:
    int b;  
};

class Student : protected Person
{
public:
    int c;
};

int main()
{
    Student s;
    Person *pPerson;
    pPerson = &s;
    return 0;
}

C-T error:

Error is: type cast: conversion from 'Student*' to 'Person*' exists, but is inaccessible

rakamakafo
  • 1,144
  • 5
  • 21
  • 44
  • Sure, parent can technically access child, but How would parent keep track of children? – Ess Kay May 08 '18 at 18:27
  • 2
    Please don't talk about "parent" and "child" - in C++ we talk about "base" and "derived", and there is no way for a base class to know about the features of a derived class, except for those that are also in the base class. –  May 08 '18 at 18:28
  • But there all methods are static (aka no virtual functions). In above assignment , even if derivation was public, there is no way Person could change or access anything in Student – rakamakafo May 08 '18 at 18:29
  • 3
    @NeilButterworth , please don't lead post to unrelated topic, I think they are synonymous. And I don't think that any C++ people would be confused with Parent/Child instead of Base/Derived. – rakamakafo May 08 '18 at 18:33
  • There are indeed cases where parent can access child properties, not necessarily in this example, but in others. An example is to create an object of parent inside the child, then set that object to the child's parent. The child will then be able to access parent variables..etc or vice versa – Ess Kay May 08 '18 at 18:50
  • @Ess There are such cases when the two classes are very tightly bound. These cases are always instances of bad design. –  May 08 '18 at 18:54
  • @NeilButterworth - sometimes one must think outside the 'optimal design' box – Ess Kay May 08 '18 at 18:55
  • @Ess Never happened for me - but please, post an answer illustrating what you are talking about and possibly also answering the OP's question. –  May 08 '18 at 18:57
  • Personally I do much Child to parent & Parent to child calls and references. Might not be best practice, but sometimes there are no better ways around it without rewriting several million lines of code – Ess Kay May 08 '18 at 18:58
  • could you please clarify what you're asking? Could you explain what is either confusing or desired out of your code sample? "*Here Student class itself becomes protected I guess, but why?*" needs clarification – kmdreko May 08 '18 at 19:00
  • @vu1p3n0x , the question is why I can't do this pPerson = &s; in above example. I don't want to fix code. I just want to learn the reason why protected derivation makes this – rakamakafo May 08 '18 at 19:02
  • 1
    Ah. That can be answered cleanly since you've already read the question I would use as a duplicate. `main` is not a member of the inheritance hierarchy so it does not know about the relationship between `Student` and `Person`. Only `Student` and classes derived from `Student` know that `Student` is derived from `Person`. – user4581301 May 08 '18 at 19:06
  • @user4581301 , hmm, so in derivation we are not just defining how members are inherited (aka how classes sees parent members), but also define how classes theirselves see each other , right? Or am I talking about same things? – rakamakafo May 08 '18 at 19:21
  • Protected and Private inheritance works exactly the same as protected and private member declaration. – Gem Taylor May 08 '18 at 19:28
  • @GemTaylor, but if make all members protected in Student , and derivation public, things work – rakamakafo May 08 '18 at 19:29
  • @user4581301 , I put an exception in code. Could you please explain like what type of conversion is that and where from it wants to call it? Maybe that will make things clear? – rakamakafo May 08 '18 at 19:30
  • You are defining how the rest of the program sees the inheritance. `main` cannot assign a `Student` to a `Person` without complaint (you've seen that in the error message above) and cannot access the `public` members of `Person` through a `Student`. Only Student or a a class derived from `Student` can. – user4581301 May 08 '18 at 19:37
  • @Sher but if you made member c protected, you would not be able to access the c part of Student from main. You made Person a protected base of Student and now you cannot access the person part of Student it from main. It works exactly the same. – Gem Taylor May 08 '18 at 19:52

1 Answers1

3

Its not about how Person "sees" Student, its about what access control on inheritance means.

When you say class Student: public Person, it means you are announcing that a Student is a Person to everyone, that means main() knows a Student* can be referred to by a Person*. So all is good.

When you say class Student: private Person, it means Student is inheriting functionality from Person, but that is simply an implementation detail. This doesn't let anyone know a Student is a Person so it can't be considered as one. Therefore main() thinks Student* and Person* are unrelated.

When you say class Student: protected Person, its a little bit trickier but the process still applies. You are inheriting the functionality from Person and any derived class of Student also knows that. So if you had a class like Freshman that inherited from Student, it'd know it was also a Person. However, that is specific to the inherited classes, main() doesn't know Student is a Person because that knowledge is protected.

kmdreko
  • 42,554
  • 6
  • 57
  • 106