Inheritance, "public protected and private" confusion

+1 Aadesh Rana · April 24, 2015
when we create the derived class we use the following code 

class Daughter: public Mother;

what does the keyword public signify over here?

Post a Reply


Oldest  Newest  Rating
0 Dol Lod · April 24, 2015
Public means you are inheriting all public variables and functions from the class Mother. In other words, Daughter can use Mother's functions and any public variables, but can't use anything private to Mother. 
0 Dol Lod · April 24, 2015

What I wrote was how public works in the way that it was used. 

There's nothing wrong in what I said. I didn't discuss private or protected. I simply clarified what using the public would do in the given code above.

Protected means daughter can directly access access private members and functions of Mother. 

Private is as you say it is. It means the public and protected features of Mother would be accessible by the daughter class, not descendants of the daughter class. 
0 Dol Lod · April 25, 2015
Daughter in the given code above would inherit public methods and variables from Mother that it can actually access or use. 
0 Dol Lod · April 25, 2015
Now, I can actually see what you meant by that comment. You're right, I should have been a bit more clear about what I meant. 

For clarification, what I meant was that Daughter will inherit everything from Mother, but the only features of Mother that Daughter can directly call or access are public member variables and methods. Everything else is hidden behind the scenes when using public. 
0 Aadesh Rana · May 8, 2015
That just made it more confusing. In inheritance you inherit all the public and protected data members and functions right? So could you guys tell me what happens when we use that code

class Daughter: public Mother;

And what would happen if we changed that to private or protected

0 Michael Bradford · May 8, 2015
So the thing I feel people have left out in their explanations is the idea that a public inheritance is an "IS-A" type inheritance, what that means is that when you inherit publicly like in your example of mother and daughter that if in my client code I decide to use the daughter class, but I send it in to a function that's only argument is a mother object, no errors will occur because with public "IS-A" inheritance a derived class is able to be treated as if it were a parent class (in this case a daughter object can be used as a mother object). When you change the inheritance to protected/private then the client code doesn't know that daughter can be used as a mother object and the code I mentioned earlier would throw problems.

I hope that wasn't EVEN more confusing but yeah that's an important bit in this that might help alleviate confusion.
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator