Simple inheritance in C ++

I collected some trial questions for the upcoming test, and this question is completely confusing for me.

Consider the following code:

class GraduateStudent : public Student { ... }; 

If the word "public" is omitted, GraduateStudent uses private inheritance, which means which of the following?

  • GraduateStudent objects may not use Student methods.

  • GraduateStudent does not have access to Student private objects.

  • No GraduateStudent method can call the Student method.

  • Only const GraduateStudent methods can call Student methods.

+7
c ++
source share
4 answers

Although this is a bare question about homework, I am going to answer it because it is a terrible question. I would almost consider this a focal question, and he really does not do it for a good test of knowledge.

Answer 2. GraduateStudent does not have access to Student private objects. , except that it has nothing to do with private inheritance. Point 2 would be true if the public keyword were present, since derived classes never had access to the private members of their base classes, regardless of how they inherit.

Private inheritance essentially means two things (as opposed to public inheritance):

  • All public Student methods become private methods in GraduateStudent . This means that if, for example, Student has a public method foo() , then GraduateStudent has a private method foo() .

  • The base class is "unavailable", which means that polymorphism is not working. In layman terms, this means that if a GraduateStudent inherited privately from Student , then you cannot handle GraduateStudent* as if it were Student* (or a GraduateStudent& , as if it were Student& ).

It is possible that the author of the question also meant that point 1 is the correct answer, but it is ambiguously formulated. What does it mean that GraduateStudent objects may not use Student methods? Perhaps this means that you cannot call methods inherited from Student for objects like GraduateStudent , as I wrote in the first paragraph above, but the GraduateStudent object itself , within its methods, may use Student methods.

For example:

 class Student { public: void foo() {}; }; class GraduateStudent : Student { public: void bar() { foo(); // Legal } }; int main() { GraduateStudent g; g.bar(); // Legal g.foo(); // Illegal return 0; }; 
+22
source share

The only correct answer I see is 2, but it does not depend on whether GraduateStudent inherited from Student privately or publicly.

Private inheritance means that it is personal knowledge that GraduateStudent is Student , so only friends of GraduateStudent and GraduateStudent members can static_cast GraduateStudent& to Student& and GraduateStudent* to Student* , access public or protected Student member variables and call public or protected functions- Student members.

See: http://www.parashift.com/c++-faq-lite/private-inheritance.html

+3
source share
 1. GraduateStudent objects may not use methods of Student. 

False, all GraduateStudent objects can be used by any public or protected student members (obviously, private members are an exception here) internally. Also, any outsider using these objects cannot access the Student's base class; access to the base class must occur in the context of GraduateStudent methods.

 2. GraduateStudent does not have access to private objects of Student. 

Yes

 3. No method of GraduateStudent may call a method of Student. 

Not true

 4. Only const methods of GraduateStudent can call methods of Student. 

No, there would be no difference in constant members that have greater access to the base class than non-constant members.

+3
source share

First: the inheritance access specifier does not change the way Derived interacts with Base , it changes the way the Derived object is treated as a world, as if it were Base .

Consider:

 struct A{}; struct B: xxxx A { friend void friendly(); }; struct C: B {}; void outsider(); 

A very simple table: I summarize which zones A different participants can access.

 xxxx public protected private B pub/prot pub/prot pub/prot friendly pub/prot pub/prot pub/prot * C pub/prot pub/prot -- outsider pub -- -- 

Note (*): A friend has the same rights as the object itself, not surprisingly.

A simple rule is to consider that the inheritance access specifier overrides the base class access specifiers, if they are more free.

Since nothing is less than public , this does not change anything. protected means that the public Base section becomes protected , and private means that the public and protected sections become private .

+1
source share

All Articles