Sign in to follow this  
Experiment-626

Protected vs. Private in inheritance

Recommended Posts

I want to make screen objects/entities branch from a single class: Base screen object -> blah -> blah -> spaceship -> etc. Should I make the private variables protected so I do not have to make new public functions in the inherited child class just to access the parent's private variables? If I am not mistaken, public are like global, private are only accessible to the class and not its children, and protected are like private but its children can access as if they were public.

Share this post


Link to post
Share on other sites
In general, protected member data is a bad idea. If you make it protected you then push the burden of maintaining class invariants onto the child classes rather than centrally managing the class invariants. Note that this argument doesn't apply to protected member functions.

Share this post


Link to post
Share on other sites
You are correct about how public, private and protected works. If there is no need for the children to access a variable it's best to leave it as private. I have heard from some people that it's better to have protected functions instead of protected variables. I don't have my own opinion on this yet.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1312760029' post='4845955']
In general, protected member data is a bad idea. If you make it protected you then push the burden of maintaining class invariants onto the child classes rather than centrally managing the class invariants. Note that this argument doesn't apply to protected member functions.
[/quote]

I do not understand what you mean by this: maintaining class invariants onto the child classes rather than centrally managing the class invariants. I do not understand what an invariant is.

Share this post


Link to post
Share on other sites
I must be thick. If the child inherits the parent publicly then the public functions are public to the child. If I were to have a private inheritance then I would need to make new functions to access the public functions. Am I correct in this? I haven't made an inherited class in a while.

Share this post


Link to post
Share on other sites
A class invariant is a statement or set of statements about a class that must always be true for objects of that type to be in a valid state. The classic example is a Date class with a month, a day and a year member variables. The month needs to be a number from 1 to 12. If the month is 1 then the day needs to be a number from 1 to 31. If the day variable becomes 76 then the class will be in an invalid state. If these are public variables then you'd need to trust every single user of the class to make sure that the date is valid. However, if you make them private variables then all you need to do is make sure that all the member functions of the class satisfy the invariant. Similarly, if a variable is protected, then not only do you need to make sure that all the member functions of the class respect the invariant, you need to make sure that every single member function of every single derived class respects the invariant.

Share this post


Link to post
Share on other sites
[quote name='Experiment-626' timestamp='1312759498' post='4845953'] Should I make the private variables protected so I do not have to make new public functions in the inherited [s]child [/s]class just to access the parent's private variables?[/quote]

In short: No.

You need to decide if your "base screen object" is really an object, or if it's just a very common collection of data. If it's just data, then make it public (i.e. make it a struct), and make spaceship contain one. If it's an object, then other classes should generally depend only on its public interface.

If you decide it's a base class object, and "screen objects" need special access that should be denied to others, then make protected member functions for that.

Share this post


Link to post
Share on other sites
[quote name='VReality' timestamp='1312784242' post='4846071']
[quote name='Experiment-626' timestamp='1312759498' post='4845953'] Should I make the private variables protected so I do not have to make new public functions in the inherited [s]child [/s]class just to access the parent's private variables?[/quote]
In short: No.

You need to decide if your "base screen object" is really an object, or if it's just a very common collection of data. If it's just data, then make it public (i.e. make it a struct), and make spaceship contain one. If it's an object, then other classes should generally depend only on its public interface.

If you decide it's a base class object, and "screen objects" need special access that should be denied to others, then make protected member functions for that.
[/quote]

Thank you for this input. I did not consider this option. I was just recently trying to separate a spaceship and a missile class only because the spaceship and the missile behave differently in their velocity function (at least in my design [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]) and I was having difficulty in the base. All of my on screen objects will have pixel coordinates and I was originally trying to make that part of the base class instead of making it a common struct.

Share this post


Link to post
Share on other sites
[i]maybe these will help explain it( cant get italics off <_< )

[url="http://www.codersource.net/c/c-tutorials/c-tutorial-on-inheritance.aspx"]C++ Tutorial on Inheritence[/url]
[url="http://www.cplusplus.com/doc/tutorial/inheritance/"]Friendship and inheritence[/url]
[url="http://www.cs.bu.edu/teaching/cpp/inheritance/intro/"]Introduction to inheritence[/url]
[url="http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/"]Basic inheritence[/url]
[/i]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this