+ 1

In trouble with inheritance and C++

I'm in trouble with the following code. Why class mother is inaccessible from class granddaughter if class daughter (from which granddaughter inherits) inherits from class mother with the access specifier set to PRIVATE? I can't create objects nor pointers. Why? What is the logic behind this behavior? What could be the problems if I use a mother object in the granddaughter class? And, connected to this last question, why if class daughter inherits from class mother with the access specifier set to PUBLIC or PROTECTED I can create a mother object into granddaughter class? Do those problems not come back to me? #include <iostream> #include <stdio.h> using namespace std; class mother { public: mother(){}; }; class daughter : private mother { public: daughter(){}; }; class granddaughter : public daughter { public: granddaughter(){}; mother _mother; // ERROR mother* mother; // ERROR }; int main(void){return 0;}

11th Jul 2017, 9:55 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
5 odpowiedzi
+ 2
@Jamie On the top right of a message there's those 3 dots in a column. If you click that and click "copy text" it'll copy the entire message. You'll have to remove the non-code stuff though.
11th Jul 2017, 10:19 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
But constructors and destructors are never inherited and when I create a pointer to a mother object I do not call mother constructor.
11th Jul 2017, 10:22 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
0
@andrea, as you have inherited the class mother as private, you can't access that call through the daughter's class. because private was meant to be used only by the current class. though you instantiated it publically. to access it, SIMPLY getter method that return mother object. try this: under public identifier, mother getMother(){ return _mother;} by doing this I am sure you can access it from out side that class,
11th Jul 2017, 11:58 PM
Nura Programmer
Nura Programmer - avatar